From: Jason Gunthorpe <jgg@nvidia.com>
To: Alex Williamson <alex.williamson@redhat.com>,
Cornelia Huck <cohuck@redhat.com>,
kvm@vger.kernel.org
Cc: Christoph Hellwig <hch@lst.de>,
"Tian, Kevin" <kevin.tian@intel.com>,
Liu Yi L <yi.l.liu@intel.com>
Subject: [PATCH v3 3/5] vfio: Don't leak a group reference if the group already exists
Date: Fri, 15 Oct 2021 08:40:52 -0300 [thread overview]
Message-ID: <3-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> (raw)
In-Reply-To: <0-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com>
If vfio_create_group() searches the group list and returns an already
existing group it does not put back the iommu_group reference that the
caller passed in.
Change the semantic of vfio_create_group() to not move the reference in
from the caller, but instead obtain a new reference inside and leave the
caller's reference alone. The two callers must now call iommu_group_put().
This is an unlikely race as the only caller that could hit it has already
searched the group list before attempting to create the group.
Fixes: cba3345cc494 ("vfio: VFIO core")
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/vfio/vfio.c | 22 ++++++++--------------
1 file changed, 8 insertions(+), 14 deletions(-)
diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index 513fb5a4c102db..4abb2e5e196536 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -334,6 +334,7 @@ static void vfio_group_unlock_and_free(struct vfio_group *group)
list_del(&unbound->unbound_next);
kfree(unbound);
}
+ iommu_group_put(group->iommu_group);
kfree(group);
}
@@ -385,12 +386,15 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
atomic_set(&group->opened, 0);
init_waitqueue_head(&group->container_q);
group->iommu_group = iommu_group;
+ /* put in vfio_group_unlock_and_free() */
+ iommu_group_ref_get(iommu_group);
group->type = type;
BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
group->nb.notifier_call = vfio_iommu_group_notifier;
ret = iommu_group_register_notifier(iommu_group, &group->nb);
if (ret) {
+ iommu_group_put(iommu_group);
kfree(group);
return ERR_PTR(ret);
}
@@ -426,7 +430,6 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
list_add(&group->vfio_next, &vfio.group_list);
mutex_unlock(&vfio.group_lock);
-
return group;
}
@@ -434,7 +437,6 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
static void vfio_group_release(struct kref *kref)
{
struct vfio_group *group = container_of(kref, struct vfio_group, kref);
- struct iommu_group *iommu_group = group->iommu_group;
/*
* These data structures all have paired operations that can only be
@@ -450,7 +452,6 @@ static void vfio_group_release(struct kref *kref)
list_del(&group->vfio_next);
vfio_free_group_minor(group->minor);
vfio_group_unlock_and_free(group);
- iommu_group_put(iommu_group);
}
static void vfio_group_put(struct vfio_group *group)
@@ -735,7 +736,7 @@ static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,
ret = PTR_ERR(group);
goto out_remove_device;
}
-
+ iommu_group_put(iommu_group);
return group;
out_remove_device:
@@ -770,18 +771,11 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
if (!iommu_group)
return ERR_PTR(-EINVAL);
- /* a found vfio_group already holds a reference to the iommu_group */
group = vfio_group_get_from_iommu(iommu_group);
- if (group)
- goto out_put;
+ if (!group)
+ group = vfio_create_group(iommu_group, VFIO_IOMMU);
- /* a newly created vfio_group keeps the reference. */
- group = vfio_create_group(iommu_group, VFIO_IOMMU);
- if (IS_ERR(group))
- goto out_put;
- return group;
-
-out_put:
+ /* The vfio_group holds a reference to the iommu_group */
iommu_group_put(iommu_group);
return group;
}
--
2.33.0
next prev parent reply other threads:[~2021-10-15 11:40 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-15 11:40 [PATCH v3 0/5] Update vfio_group to use the modern cdev lifecycle Jason Gunthorpe
2021-10-15 11:40 ` [PATCH v3 1/5] vfio: Delete vfio_get/put_group from vfio_iommu_group_notifier() Jason Gunthorpe
2021-10-15 11:40 ` [PATCH v3 2/5] vfio: Do not open code the group list search in vfio_create_group() Jason Gunthorpe
2021-10-15 11:40 ` Jason Gunthorpe [this message]
2021-10-15 11:40 ` [PATCH v3 4/5] vfio: Use a refcount_t instead of a kref in the vfio_group Jason Gunthorpe
2021-10-15 11:40 ` [PATCH v3 5/5] vfio: Use cdev_device_add() instead of device_create() Jason Gunthorpe
2021-10-15 13:20 ` [PATCH v3 0/5] Update vfio_group to use the modern cdev lifecycle Christoph Hellwig
2021-10-20 19:52 ` Alex Williamson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com \
--to=jgg@nvidia.com \
--cc=alex.williamson@redhat.com \
--cc=cohuck@redhat.com \
--cc=hch@lst.de \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=yi.l.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).