All of lore.kernel.org
 help / color / mirror / Atom feed
From: Diana Craciun OSS <diana.craciun@oss.nxp.com>
To: Jason Gunthorpe <jgg@nvidia.com>,
	Cornelia Huck <cohuck@redhat.com>,
	kvm@vger.kernel.org
Cc: Alex Williamson <alex.williamson@redhat.com>,
	"Raj, Ashok" <ashok.raj@intel.com>,
	Bharat Bhushan <Bharat.Bhushan@nxp.com>,
	Dan Williams <dan.j.williams@intel.com>,
	Daniel Vetter <daniel@ffwll.ch>,
	Eric Auger <eric.auger@redhat.com>,
	Christoph Hellwig <hch@lst.de>,
	Leon Romanovsky <leonro@nvidia.com>,
	Max Gurtovoy <mgurtovoy@nvidia.com>,
	Tarun Gupta <targupta@nvidia.com>,
	Laurentiu Tudor <laurentiu.tudor@nxp.com>
Subject: Re: [PATCH v2 05/14] vfio/fsl-mc: Re-order vfio_fsl_mc_probe()
Date: Wed, 17 Mar 2021 18:36:09 +0200	[thread overview]
Message-ID: <ddaf1e9a-5f8a-e28d-9f19-928ccf9a15b5@oss.nxp.com> (raw)
In-Reply-To: <5-v2-20d933792272+4ff-vfio1_jgg@nvidia.com>

Hi,

Thanks for finding this!

I tested the series and currently the binding to vfio fails. The reason 
is that it is assumed that the objects scan is done after 
vfio_add_group_dev. But at this point the vdev structure is completly 
initialized.

I'll add some more context.

There are two types of FSL MC devices:
- a DPRC device
- regular devices

A DPRC is some kind of container of the other devices. The DPRC VFIO 
device is scanning for all the existing devices in the container and 
triggers the probe function for those devices. However, there are some 
pieces of code that needs to be protected by a lock, lock that is 
created by vfio_fsl_mc_reflck_attach() function. This function is 
searching for the DPRC vdev (having the physical device) in the vfio 
group, so the "parent" device should have been added in the group before 
the child devices are probed.


I did some changes on top of these series and this is how they look 
like. I hope that I do not do something that violates the way the VFIO 
is designed.

diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c 
b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
index 3af3ca59478f..9b4c9356515a 100644
--- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c
+++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
@@ -578,22 +578,32 @@ static int vfio_fsl_mc_init_device(struct 
vfio_fsl_mc_device *vdev)
                 goto out_nc_unreg;
         }

-       ret = dprc_scan_container(mc_dev, false);
-       if (ret) {
-               dev_err(&mc_dev->dev, "VFIO_FSL_MC: Container scanning 
failed (%d)\n", ret);
-               goto out_dprc_cleanup;
-       }
-
         return 0;

-out_dprc_cleanup:
-       dprc_remove_devices(mc_dev, NULL, 0);
-       dprc_cleanup(mc_dev);
  out_nc_unreg:
         bus_unregister_notifier(&fsl_mc_bus_type, &vdev->nb);
         return ret;
  }

+static int vfio_fsl_mc_scan_container(struct vfio_fsl_mc_device *vdev)
+{
+       struct fsl_mc_device *mc_dev = vdev->mc_dev;
+       int ret;
+
+       /* non dprc devices do not scan for other devices */
+       if (is_fsl_mc_bus_dprc(mc_dev)) {
+               ret = dprc_scan_container(mc_dev, false);
+               if (ret) {
+                       dev_err(&mc_dev->dev, "VFIO_FSL_MC: Container 
scanning failed (%d)\n", ret);
+                       dprc_remove_devices(mc_dev, NULL, 0);
+                       return ret;
+               }
+       }
+
+       return 0;
+}
+
+
  static void vfio_fsl_uninit_device(struct vfio_fsl_mc_device *vdev)
  {
         struct fsl_mc_device *mc_dev = vdev->mc_dev;
@@ -642,9 +652,16 @@ static int vfio_fsl_mc_probe(struct fsl_mc_device 
*mc_dev)
                 dev_err(dev, "VFIO_FSL_MC: Failed to add to vfio group\n");
                 goto out_device;
         }
+
+       ret = vfio_fsl_mc_scan_container(vdev);
+       if (ret)
+               goto out_group_dev;
+
         dev_set_drvdata(dev, vdev);
         return 0;

+out_group_dev:
+       vfio_unregister_group_dev(&vdev->vdev);
  out_device:
         vfio_fsl_uninit_device(vdev);
  out_reflck:


Thanks,
Diana

On 3/13/2021 2:55 AM, Jason Gunthorpe wrote:
> vfio_add_group_dev() must be called only after all of the private data in
> vdev is fully setup and ready, otherwise there could be races with user
> space instantiating a device file descriptor and starting to call ops.
> 
> For instance vfio_fsl_mc_reflck_attach() sets vdev->reflck and
> vfio_fsl_mc_open(), called by fops open, unconditionally derefs it, which
> will crash if things get out of order.
> 
> This driver started life with the right sequence, but three commits added
> stuff after vfio_add_group_dev().
> 
> Fixes: 2e0d29561f59 ("vfio/fsl-mc: Add irq infrastructure for fsl-mc devices")
> Fixes: f2ba7e8c947b ("vfio/fsl-mc: Added lock support in preparation for interrupt handling")
> Fixes: 704f5082d845 ("vfio/fsl-mc: Scan DPRC objects on vfio-fsl-mc driver bind")
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>   drivers/vfio/fsl-mc/vfio_fsl_mc.c | 43 ++++++++++++++++---------------
>   1 file changed, 22 insertions(+), 21 deletions(-)
> 
> diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
> index f27e25112c4037..881849723b4dfb 100644
> --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c
> +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
> @@ -582,11 +582,21 @@ static int vfio_fsl_mc_init_device(struct vfio_fsl_mc_device *vdev)
>   	dprc_cleanup(mc_dev);
>   out_nc_unreg:
>   	bus_unregister_notifier(&fsl_mc_bus_type, &vdev->nb);
> -	vdev->nb.notifier_call = NULL;
> -
>   	return ret;
>   }
>   
> +static void vfio_fsl_uninit_device(struct vfio_fsl_mc_device *vdev)
> +{
> +	struct fsl_mc_device *mc_dev = vdev->mc_dev;
> +
> +	if (!is_fsl_mc_bus_dprc(mc_dev))
> +		return;
> +
> +	dprc_remove_devices(mc_dev, NULL, 0);
> +	dprc_cleanup(mc_dev);
> +	bus_unregister_notifier(&fsl_mc_bus_type, &vdev->nb);
> +}
> +
>   static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
>   {
>   	struct iommu_group *group;
> @@ -607,29 +617,27 @@ static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
>   	}
>   
>   	vdev->mc_dev = mc_dev;
> -
> -	ret = vfio_add_group_dev(dev, &vfio_fsl_mc_ops, vdev);
> -	if (ret) {
> -		dev_err(dev, "VFIO_FSL_MC: Failed to add to vfio group\n");
> -		goto out_group_put;
> -	}
> +	mutex_init(&vdev->igate);
>   
>   	ret = vfio_fsl_mc_reflck_attach(vdev);
>   	if (ret)
> -		goto out_group_dev;
> +		goto out_group_put;
>   
>   	ret = vfio_fsl_mc_init_device(vdev);
>   	if (ret)
>   		goto out_reflck;
>   
> -	mutex_init(&vdev->igate);
> -
> +	ret = vfio_add_group_dev(dev, &vfio_fsl_mc_ops, vdev);
> +	if (ret) {
> +		dev_err(dev, "VFIO_FSL_MC: Failed to add to vfio group\n");
> +		goto out_device;
> +	}
>   	return 0;
>   
> +out_device:
> +	vfio_fsl_uninit_device(vdev);
>   out_reflck:
>   	vfio_fsl_mc_reflck_put(vdev->reflck);
> -out_group_dev:
> -	vfio_del_group_dev(dev);
>   out_group_put:
>   	vfio_iommu_group_put(group, dev);
>   	return ret;
> @@ -646,16 +654,9 @@ static int vfio_fsl_mc_remove(struct fsl_mc_device *mc_dev)
>   
>   	mutex_destroy(&vdev->igate);
>   
> +	vfio_fsl_uninit_device(vdev);
>   	vfio_fsl_mc_reflck_put(vdev->reflck);
>   
> -	if (is_fsl_mc_bus_dprc(mc_dev)) {
> -		dprc_remove_devices(mc_dev, NULL, 0);
> -		dprc_cleanup(mc_dev);
> -	}
> -
> -	if (vdev->nb.notifier_call)
> -		bus_unregister_notifier(&fsl_mc_bus_type, &vdev->nb);
> -
>   	vfio_iommu_group_put(mc_dev->dev.iommu_group, dev);
>   
>   	return 0;
> 


  parent reply	other threads:[~2021-03-17 16:37 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-13  0:55 [PATCH v2 00/14] Embed struct vfio_device in all sub-structures Jason Gunthorpe
2021-03-13  0:55 ` [PATCH v2 01/14] vfio: Remove extra put/gets around vfio_device->group Jason Gunthorpe
2021-03-16  7:33   ` Tian, Kevin
2021-03-16 23:07     ` Jason Gunthorpe
2021-03-17  0:47       ` Tian, Kevin
2021-03-19 13:58         ` Jason Gunthorpe
2021-03-16 11:15   ` Max Gurtovoy
2021-03-16 11:59   ` Cornelia Huck
2021-03-18  9:32   ` Auger Eric
2021-03-13  0:55 ` [PATCH v2 02/14] vfio: Simplify the lifetime logic for vfio_device Jason Gunthorpe
2021-03-16  7:38   ` Tian, Kevin
2021-03-16 12:10     ` Cornelia Huck
2021-03-16 20:24     ` Alex Williamson
2021-03-16 23:08       ` Jason Gunthorpe
2021-03-17  8:12       ` Cornelia Huck
2021-03-23 13:06         ` Jason Gunthorpe
2021-03-18 13:10   ` Auger Eric
2021-03-13  0:55 ` [PATCH v2 03/14] vfio: Split creation of a vfio_device into init and register ops Jason Gunthorpe
2021-03-16  7:55   ` Tian, Kevin
2021-03-16 13:34     ` Jason Gunthorpe
2021-03-17  0:55       ` Tian, Kevin
2021-03-16 12:25   ` Cornelia Huck
2021-03-16 21:13     ` Alex Williamson
2021-03-16 23:12       ` Jason Gunthorpe
2021-03-16 12:54   ` Max Gurtovoy
2021-03-18 13:18   ` Auger Eric
2021-03-13  0:55 ` [PATCH v2 04/14] vfio/platform: Use vfio_init/register/unregister_group_dev Jason Gunthorpe
2021-03-16 16:22   ` Cornelia Huck
2021-03-16 21:33   ` Alex Williamson
2021-03-16 21:45     ` Jason Gunthorpe
2021-03-18 13:40   ` Auger Eric
2021-03-13  0:55 ` [PATCH v2 05/14] vfio/fsl-mc: Re-order vfio_fsl_mc_probe() Jason Gunthorpe
2021-03-15  8:44   ` Christoph Hellwig
2021-03-16  9:16   ` Diana Craciun OSS
2021-03-16 16:28   ` Cornelia Huck
2021-03-17 16:36   ` Diana Craciun OSS [this message]
2021-03-17 22:59     ` Jason Gunthorpe
2021-03-13  0:55 ` [PATCH v2 06/14] vfio/fsl-mc: Use vfio_init/register/unregister_group_dev Jason Gunthorpe
2021-03-15  8:44   ` Christoph Hellwig
2021-03-16 16:43   ` Cornelia Huck
2021-03-13  0:55 ` [PATCH v2 07/14] vfio/pci: Move VGA and VF initialization to functions Jason Gunthorpe
2021-03-15  8:45   ` Christoph Hellwig
2021-03-15 23:07     ` Jason Gunthorpe
2021-03-16  6:27       ` Christoph Hellwig
2021-03-16  7:57   ` Tian, Kevin
2021-03-16 13:02   ` Max Gurtovoy
2021-03-16 23:04     ` Jason Gunthorpe
2021-03-16 16:51   ` Cornelia Huck
2021-03-18 16:34   ` Auger Eric
2021-03-13  0:56 ` [PATCH v2 08/14] vfio/pci: Re-order vfio_pci_probe() Jason Gunthorpe
2021-03-15  8:46   ` Christoph Hellwig
2021-03-16  8:04   ` Tian, Kevin
2021-03-16 13:20     ` Jason Gunthorpe
2021-03-16 22:27       ` Alex Williamson
2021-03-17  0:56         ` Tian, Kevin
2021-03-16 11:28   ` Max Gurtovoy
2021-03-17 10:32   ` Cornelia Huck
2021-03-18 16:50   ` Auger Eric
2021-03-13  0:56 ` [PATCH v2 09/14] vfio/pci: Use vfio_init/register/unregister_group_dev Jason Gunthorpe
2021-03-16  8:06   ` Tian, Kevin
2021-03-17 10:33   ` Cornelia Huck
2021-03-18 13:43   ` Auger Eric
2021-03-13  0:56 ` [PATCH v2 10/14] vfio/mdev: " Jason Gunthorpe
2021-03-16  8:09   ` Tian, Kevin
2021-03-16 22:51     ` Alex Williamson
2021-03-16 23:19     ` Jason Gunthorpe
2021-03-17 10:36   ` Cornelia Huck
2021-03-13  0:56 ` [PATCH v2 11/14] vfio/mdev: Make to_mdev_device() into a static inline Jason Gunthorpe
2021-03-16  8:10   ` Tian, Kevin
2021-03-16 22:55   ` Alex Williamson
2021-03-16 23:20     ` Jason Gunthorpe
2021-03-17 10:36   ` Cornelia Huck
2021-03-13  0:56 ` [PATCH v2 12/14] vfio: Make vfio_device_ops pass a 'struct vfio_device *' instead of 'void *' Jason Gunthorpe
2021-03-15  8:58   ` Christoph Hellwig
2021-03-17 11:33   ` Cornelia Huck
2021-03-13  0:56 ` [PATCH v2 13/14] vfio/pci: Replace uses of vfio_device_data() with container_of Jason Gunthorpe
2021-03-16  8:20   ` Tian, Kevin
2021-03-17 12:06   ` Cornelia Huck
2021-03-13  0:56 ` [PATCH v2 14/14] vfio: Remove device_data from the vfio bus driver API Jason Gunthorpe
2021-03-16  8:22   ` Tian, Kevin
2021-03-17 12:08   ` Cornelia Huck
2021-03-17 23:24   ` Max Gurtovoy

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=ddaf1e9a-5f8a-e28d-9f19-928ccf9a15b5@oss.nxp.com \
    --to=diana.craciun@oss.nxp.com \
    --cc=Bharat.Bhushan@nxp.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=eric.auger@redhat.com \
    --cc=hch@lst.de \
    --cc=jgg@nvidia.com \
    --cc=kvm@vger.kernel.org \
    --cc=laurentiu.tudor@nxp.com \
    --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.