All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: Alex Williamson <alex.williamson@redhat.com>,
	Cornelia Huck <cohuck@redhat.com>,
	Diana Craciun <diana.craciun@oss.nxp.com>,
	kvm@vger.kernel.org
Cc: "Raj, Ashok" <ashok.raj@intel.com>,
	Dan Williams <dan.j.williams@intel.com>,
	Daniel Vetter <daniel@ffwll.ch>, Christoph Hellwig <hch@lst.de>,
	Leon Romanovsky <leonro@nvidia.com>,
	Max Gurtovoy <mgurtovoy@nvidia.com>,
	Tarun Gupta <targupta@nvidia.com>
Subject: [PATCH 04/10] vfio/fsl-mc: Use vfio_init/register/unregister_group_dev
Date: Tue,  9 Mar 2021 17:38:46 -0400	[thread overview]
Message-ID: <4-v1-7355d38b9344+17481-vfio1_jgg@nvidia.com> (raw)
In-Reply-To: <0-v1-7355d38b9344+17481-vfio1_jgg@nvidia.com>

fsl-mc already allocates a struct vfio_fsl_mc_device with exactly the same
lifetime as vfio_device, switch to the new API and embed vfio_device in
vfio_fsl_mc_device. While here remove the devm usage for the vdev, this
code is clean and doesn't need devm.

Add a note the probe ordering is racy.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/fsl-mc/vfio_fsl_mc.c         | 30 +++++++++++++----------
 drivers/vfio/fsl-mc/vfio_fsl_mc_private.h |  1 +
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
index f27e25112c4037..ddee6ed20c4523 100644
--- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c
+++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
@@ -600,20 +600,28 @@ static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
 		return -EINVAL;
 	}
 
-	vdev = devm_kzalloc(dev, sizeof(*vdev), GFP_KERNEL);
+	vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
 	if (!vdev) {
 		ret = -ENOMEM;
 		goto out_group_put;
 	}
 
+	vfio_init_group_dev(&vdev->vdev, dev, &vfio_fsl_mc_ops, vdev);
+	mutex_init(&vdev->igate);
 	vdev->mc_dev = mc_dev;
 
-	ret = vfio_add_group_dev(dev, &vfio_fsl_mc_ops, vdev);
+	ret = vfio_register_group_dev(&vdev->vdev);
 	if (ret) {
 		dev_err(dev, "VFIO_FSL_MC: Failed to add to vfio group\n");
-		goto out_group_put;
+		goto out_kfree;
 	}
+	dev_set_drvdata(dev, vdev);
 
+	/*
+	 * FIXME: vfio_register_group_dev() allows VFIO_GROUP_GET_DEVICE_FD to
+	 * immediately return the device to userspace, but we haven't finished
+	 * setting it up yet.
+	 */
 	ret = vfio_fsl_mc_reflck_attach(vdev);
 	if (ret)
 		goto out_group_dev;
@@ -621,15 +629,14 @@ static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
 	ret = vfio_fsl_mc_init_device(vdev);
 	if (ret)
 		goto out_reflck;
-
-	mutex_init(&vdev->igate);
-
 	return 0;
 
 out_reflck:
 	vfio_fsl_mc_reflck_put(vdev->reflck);
 out_group_dev:
-	vfio_del_group_dev(dev);
+	vfio_unregister_group_dev(&vdev->vdev);
+out_kfree:
+	kfree(vdev);
 out_group_put:
 	vfio_iommu_group_put(group, dev);
 	return ret;
@@ -637,13 +644,10 @@ static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
 
 static int vfio_fsl_mc_remove(struct fsl_mc_device *mc_dev)
 {
-	struct vfio_fsl_mc_device *vdev;
 	struct device *dev = &mc_dev->dev;
+	struct vfio_fsl_mc_device *vdev = dev_get_drvdata(dev);
 
-	vdev = vfio_del_group_dev(dev);
-	if (!vdev)
-		return -EINVAL;
-
+	vfio_unregister_group_dev(&vdev->vdev);
 	mutex_destroy(&vdev->igate);
 
 	vfio_fsl_mc_reflck_put(vdev->reflck);
@@ -656,8 +660,8 @@ static int vfio_fsl_mc_remove(struct fsl_mc_device *mc_dev)
 	if (vdev->nb.notifier_call)
 		bus_unregister_notifier(&fsl_mc_bus_type, &vdev->nb);
 
+	kfree(vdev);
 	vfio_iommu_group_put(mc_dev->dev.iommu_group, dev);
-
 	return 0;
 }
 
diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h b/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h
index a97ee691ed47ec..89700e00e77d10 100644
--- a/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h
+++ b/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h
@@ -36,6 +36,7 @@ struct vfio_fsl_mc_region {
 };
 
 struct vfio_fsl_mc_device {
+	struct vfio_device		vdev;
 	struct fsl_mc_device		*mc_dev;
 	struct notifier_block        nb;
 	int				refcnt;
-- 
2.30.1


  parent reply	other threads:[~2021-03-09 21:39 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-09 21:38 [PATCH 00/10] Embed struct vfio_device in all sub-structures Jason Gunthorpe
2021-03-09 21:38 ` [PATCH 01/10] vfio: Simplify the lifetime logic for vfio_device Jason Gunthorpe
2021-03-10  7:23   ` Christoph Hellwig
2021-03-12 15:41     ` Jason Gunthorpe
2021-03-12 16:32       ` Christoph Hellwig
2021-03-09 21:38 ` [PATCH 02/10] vfio: Split creation of a vfio_device into init and register ops Jason Gunthorpe
2021-03-10  7:26   ` Christoph Hellwig
2021-03-12 13:04   ` Liu, Yi L
2021-03-12 14:23     ` Jason Gunthorpe
2021-03-12 16:31       ` Christoph Hellwig
2021-03-09 21:38 ` [PATCH 03/10] vfio/platform: Use vfio_init/register/unregister_group_dev Jason Gunthorpe
2021-03-10  7:28   ` Christoph Hellwig
2021-03-12 17:00     ` Jason Gunthorpe
2021-03-09 21:38 ` Jason Gunthorpe [this message]
2021-03-10  7:30   ` [PATCH 04/10] vfio/fsl-mc: " Christoph Hellwig
2021-03-10 12:43     ` Jason Gunthorpe
2021-03-09 21:38 ` [PATCH 05/10] vfio/pci: " Jason Gunthorpe
2021-03-10  7:31   ` Christoph Hellwig
2021-03-12 12:53   ` Liu, Yi L
2021-03-12 13:58     ` Jason Gunthorpe
2021-03-09 21:38 ` [PATCH 06/10] vfio/mdev: " Jason Gunthorpe
2021-03-10  7:31   ` Christoph Hellwig
2021-03-12 13:09   ` Liu, Yi L
2021-03-09 21:38 ` [PATCH 07/10] vfio/mdev: Make to_mdev_device() into a static inline Jason Gunthorpe
2021-03-10  7:32   ` Christoph Hellwig
2021-03-09 21:38 ` [PATCH 08/10] vfio: Make vfio_device_ops pass a 'struct vfio_device *' instead of 'void *' Jason Gunthorpe
2021-03-10  5:52   ` Dan Williams
2021-03-10  6:24     ` Leon Romanovsky
2021-03-10 12:58     ` Jason Gunthorpe
2021-03-10 20:01       ` Dan Williams
2021-03-12 13:42   ` Liu, Yi L
2021-03-12 14:06     ` Jason Gunthorpe
2021-03-09 21:38 ` [PATCH 09/10] vfio/pci: Replace uses of vfio_device_data() with container_of Jason Gunthorpe
2021-03-10  7:36   ` Christoph Hellwig
2021-03-10 19:59     ` Jason Gunthorpe
2021-03-11 11:21       ` Christoph Hellwig
2021-03-12 13:42   ` Liu, Yi L
2021-03-12 14:09     ` Jason Gunthorpe
2021-03-09 21:38 ` [PATCH 10/10] vfio: Remove device_data from the vfio bus driver API Jason Gunthorpe
2021-03-10  7:37   ` Christoph Hellwig
2021-03-10 23:52 ` [PATCH 00/10] Embed struct vfio_device in all sub-structures Alex Williamson
2021-03-10 23:57   ` Jason Gunthorpe

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=4-v1-7355d38b9344+17481-vfio1_jgg@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=alex.williamson@redhat.com \
    --cc=ashok.raj@intel.com \
    --cc=cohuck@redhat.com \
    --cc=dan.j.williams@intel.com \
    --cc=daniel@ffwll.ch \
    --cc=diana.craciun@oss.nxp.com \
    --cc=hch@lst.de \
    --cc=kvm@vger.kernel.org \
    --cc=leonro@nvidia.com \
    --cc=mgurtovoy@nvidia.com \
    --cc=targupta@nvidia.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.