OSDN Git Service

vfio: Introduce a vfio_uninit_group_dev() API call
authorMax Gurtovoy <mgurtovoy@nvidia.com>
Fri, 6 Aug 2021 01:18:59 +0000 (22:18 -0300)
committerAlex Williamson <alex.williamson@redhat.com>
Wed, 11 Aug 2021 15:50:10 +0000 (09:50 -0600)
This pairs with vfio_init_group_dev() and allows undoing any state that is
stored in the vfio_device unrelated to registration. Add appropriately
placed calls to all the drivers.

The following patch will use this to add pre-registration state for the
device set.

Signed-off-by: Max Gurtovoy <mgurtovoy@nvidia.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/3-v4-9ea22c5e6afb+1adf-vfio_reflck_jgg@nvidia.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Documentation/driver-api/vfio.rst
drivers/vfio/fsl-mc/vfio_fsl_mc.c
drivers/vfio/mdev/vfio_mdev.c
drivers/vfio/pci/vfio_pci.c
drivers/vfio/platform/vfio_platform_common.c
drivers/vfio/vfio.c
include/linux/vfio.h
samples/vfio-mdev/mbochs.c
samples/vfio-mdev/mdpy.c
samples/vfio-mdev/mtty.c

index 606eed8..c663b6f 100644 (file)
@@ -255,11 +255,13 @@ vfio_unregister_group_dev() respectively::
        void vfio_init_group_dev(struct vfio_device *device,
                                struct device *dev,
                                const struct vfio_device_ops *ops);
+       void vfio_uninit_group_dev(struct vfio_device *device);
        int vfio_register_group_dev(struct vfio_device *device);
        void vfio_unregister_group_dev(struct vfio_device *device);
 
 The driver should embed the vfio_device in its own structure and call
-vfio_init_group_dev() to pre-configure it before going to registration.
+vfio_init_group_dev() to pre-configure it before going to registration
+and call vfio_uninit_group_dev() after completing the un-registration.
 vfio_register_group_dev() indicates to the core to begin tracking the
 iommu_group of the specified dev and register the dev as owned by a VFIO bus
 driver. Once vfio_register_group_dev() returns it is possible for userspace to
index 90cad10..122997c 100644 (file)
@@ -627,7 +627,7 @@ static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
 
        ret = vfio_fsl_mc_reflck_attach(vdev);
        if (ret)
-               goto out_kfree;
+               goto out_uninit;
 
        ret = vfio_fsl_mc_init_device(vdev);
        if (ret)
@@ -657,7 +657,8 @@ out_device:
        vfio_fsl_uninit_device(vdev);
 out_reflck:
        vfio_fsl_mc_reflck_put(vdev->reflck);
-out_kfree:
+out_uninit:
+       vfio_uninit_group_dev(&vdev->vdev);
        kfree(vdev);
 out_group_put:
        vfio_iommu_group_put(group, dev);
@@ -675,7 +676,7 @@ static int vfio_fsl_mc_remove(struct fsl_mc_device *mc_dev)
        dprc_remove_devices(mc_dev, NULL, 0);
        vfio_fsl_uninit_device(vdev);
        vfio_fsl_mc_reflck_put(vdev->reflck);
-
+       vfio_uninit_group_dev(&vdev->vdev);
        kfree(vdev);
        vfio_iommu_group_put(mc_dev->dev.iommu_group, dev);
 
index 39ef748..a5c77cc 100644 (file)
@@ -120,12 +120,16 @@ static int vfio_mdev_probe(struct mdev_device *mdev)
 
        vfio_init_group_dev(vdev, &mdev->dev, &vfio_mdev_dev_ops);
        ret = vfio_register_group_dev(vdev);
-       if (ret) {
-               kfree(vdev);
-               return ret;
-       }
+       if (ret)
+               goto out_uninit;
+
        dev_set_drvdata(&mdev->dev, vdev);
        return 0;
+
+out_uninit:
+       vfio_uninit_group_dev(vdev);
+       kfree(vdev);
+       return ret;
 }
 
 static void vfio_mdev_remove(struct mdev_device *mdev)
@@ -133,6 +137,7 @@ static void vfio_mdev_remove(struct mdev_device *mdev)
        struct vfio_device *vdev = dev_get_drvdata(&mdev->dev);
 
        vfio_unregister_group_dev(vdev);
+       vfio_uninit_group_dev(vdev);
        kfree(vdev);
 }
 
index 318864d..fab3715 100644 (file)
@@ -2022,7 +2022,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
        ret = vfio_pci_reflck_attach(vdev);
        if (ret)
-               goto out_free;
+               goto out_uninit;
        ret = vfio_pci_vf_init(vdev);
        if (ret)
                goto out_reflck;
@@ -2059,7 +2059,8 @@ out_vf:
        vfio_pci_vf_uninit(vdev);
 out_reflck:
        vfio_pci_reflck_put(vdev->reflck);
-out_free:
+out_uninit:
+       vfio_uninit_group_dev(&vdev->vdev);
        kfree(vdev->pm_save);
        kfree(vdev);
 out_group_put:
@@ -2077,6 +2078,7 @@ static void vfio_pci_remove(struct pci_dev *pdev)
 
        vfio_pci_vf_uninit(vdev);
        vfio_pci_reflck_put(vdev->reflck);
+       vfio_uninit_group_dev(&vdev->vdev);
        vfio_pci_vga_uninit(vdev);
 
        vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
index 703164d..bdde860 100644 (file)
@@ -667,7 +667,7 @@ int vfio_platform_probe_common(struct vfio_platform_device *vdev,
                ret = vfio_platform_of_probe(vdev, dev);
 
        if (ret)
-               return ret;
+               goto out_uninit;
 
        vdev->device = dev;
 
@@ -675,7 +675,7 @@ int vfio_platform_probe_common(struct vfio_platform_device *vdev,
        if (ret && vdev->reset_required) {
                dev_err(dev, "No reset function found for device %s\n",
                        vdev->name);
-               return ret;
+               goto out_uninit;
        }
 
        group = vfio_iommu_group_get(dev);
@@ -698,6 +698,8 @@ put_iommu:
        vfio_iommu_group_put(group, dev);
 put_reset:
        vfio_platform_put_reset(vdev);
+out_uninit:
+       vfio_uninit_group_dev(&vdev->vdev);
        return ret;
 }
 EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
@@ -708,6 +710,7 @@ void vfio_platform_remove_common(struct vfio_platform_device *vdev)
 
        pm_runtime_disable(vdev->device);
        vfio_platform_put_reset(vdev);
+       vfio_uninit_group_dev(&vdev->vdev);
        vfio_iommu_group_put(vdev->vdev.dev->iommu_group, vdev->vdev.dev);
 }
 EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
index 02cc51c..cc375df 100644 (file)
@@ -749,6 +749,11 @@ void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
 }
 EXPORT_SYMBOL_GPL(vfio_init_group_dev);
 
+void vfio_uninit_group_dev(struct vfio_device *device)
+{
+}
+EXPORT_SYMBOL_GPL(vfio_uninit_group_dev);
+
 int vfio_register_group_dev(struct vfio_device *device)
 {
        struct vfio_device *existing_device;
index a2c5b30..b0875cf 100644 (file)
@@ -61,6 +61,7 @@ extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
 
 void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
                         const struct vfio_device_ops *ops);
+void vfio_uninit_group_dev(struct vfio_device *device);
 int vfio_register_group_dev(struct vfio_device *device);
 void vfio_unregister_group_dev(struct vfio_device *device);
 extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
index 3e885be..0f15118 100644 (file)
@@ -559,6 +559,7 @@ static int mbochs_probe(struct mdev_device *mdev)
        dev_set_drvdata(&mdev->dev, mdev_state);
        return 0;
 err_mem:
+       vfio_uninit_group_dev(&mdev_state->vdev);
        kfree(mdev_state->pages);
        kfree(mdev_state->vconfig);
        kfree(mdev_state);
@@ -572,6 +573,7 @@ static void mbochs_remove(struct mdev_device *mdev)
        struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
 
        vfio_unregister_group_dev(&mdev_state->vdev);
+       vfio_uninit_group_dev(&mdev_state->vdev);
        atomic_add(mdev_state->type->mbytes, &mbochs_avail_mbytes);
        kfree(mdev_state->pages);
        kfree(mdev_state->vconfig);
index a7d4ed2..5733403 100644 (file)
@@ -235,17 +235,16 @@ static int mdpy_probe(struct mdev_device *mdev)
 
        mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL);
        if (mdev_state->vconfig == NULL) {
-               kfree(mdev_state);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto err_state;
        }
 
        fbsize = roundup_pow_of_two(type->width * type->height * type->bytepp);
 
        mdev_state->memblk = vmalloc_user(fbsize);
        if (!mdev_state->memblk) {
-               kfree(mdev_state->vconfig);
-               kfree(mdev_state);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto err_vconfig;
        }
        dev_info(dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width,
                 type->height);
@@ -260,13 +259,18 @@ static int mdpy_probe(struct mdev_device *mdev)
        mdpy_count++;
 
        ret = vfio_register_group_dev(&mdev_state->vdev);
-       if (ret) {
-               kfree(mdev_state->vconfig);
-               kfree(mdev_state);
-               return ret;
-       }
+       if (ret)
+               goto err_mem;
        dev_set_drvdata(&mdev->dev, mdev_state);
        return 0;
+err_mem:
+       vfree(mdev_state->memblk);
+err_vconfig:
+       kfree(mdev_state->vconfig);
+err_state:
+       vfio_uninit_group_dev(&mdev_state->vdev);
+       kfree(mdev_state);
+       return ret;
 }
 
 static void mdpy_remove(struct mdev_device *mdev)
@@ -278,6 +282,7 @@ static void mdpy_remove(struct mdev_device *mdev)
        vfio_unregister_group_dev(&mdev_state->vdev);
        vfree(mdev_state->memblk);
        kfree(mdev_state->vconfig);
+       vfio_uninit_group_dev(&mdev_state->vdev);
        kfree(mdev_state);
 
        mdpy_count--;
index 8b26fec..37cc906 100644 (file)
@@ -718,8 +718,8 @@ static int mtty_probe(struct mdev_device *mdev)
 
        mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
        if (mdev_state == NULL) {
-               atomic_add(nr_ports, &mdev_avail_ports);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto err_nr_ports;
        }
 
        vfio_init_group_dev(&mdev_state->vdev, &mdev->dev, &mtty_dev_ops);
@@ -732,9 +732,8 @@ static int mtty_probe(struct mdev_device *mdev)
        mdev_state->vconfig = kzalloc(MTTY_CONFIG_SPACE_SIZE, GFP_KERNEL);
 
        if (mdev_state->vconfig == NULL) {
-               kfree(mdev_state);
-               atomic_add(nr_ports, &mdev_avail_ports);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto err_state;
        }
 
        mutex_init(&mdev_state->ops_lock);
@@ -743,14 +742,19 @@ static int mtty_probe(struct mdev_device *mdev)
        mtty_create_config_space(mdev_state);
 
        ret = vfio_register_group_dev(&mdev_state->vdev);
-       if (ret) {
-               kfree(mdev_state);
-               atomic_add(nr_ports, &mdev_avail_ports);
-               return ret;
-       }
-
+       if (ret)
+               goto err_vconfig;
        dev_set_drvdata(&mdev->dev, mdev_state);
        return 0;
+
+err_vconfig:
+       kfree(mdev_state->vconfig);
+err_state:
+       vfio_uninit_group_dev(&mdev_state->vdev);
+       kfree(mdev_state);
+err_nr_ports:
+       atomic_add(nr_ports, &mdev_avail_ports);
+       return ret;
 }
 
 static void mtty_remove(struct mdev_device *mdev)
@@ -761,6 +765,7 @@ static void mtty_remove(struct mdev_device *mdev)
        vfio_unregister_group_dev(&mdev_state->vdev);
 
        kfree(mdev_state->vconfig);
+       vfio_uninit_group_dev(&mdev_state->vdev);
        kfree(mdev_state);
        atomic_add(nr_ports, &mdev_avail_ports);
 }