vfio: Introduce a vfio_uninit_group_dev() API call

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>
This commit is contained in:
Max Gurtovoy 2021-08-05 22:18:59 -03:00 committed by Alex Williamson
parent de5494af48
commit ae03c3771b
10 changed files with 64 additions and 33 deletions

View File

@ -255,11 +255,13 @@ vfio_unregister_group_dev() respectively::
void vfio_init_group_dev(struct vfio_device *device, void vfio_init_group_dev(struct vfio_device *device,
struct device *dev, struct device *dev,
const struct vfio_device_ops *ops); const struct vfio_device_ops *ops);
void vfio_uninit_group_dev(struct vfio_device *device);
int vfio_register_group_dev(struct vfio_device *device); int vfio_register_group_dev(struct vfio_device *device);
void vfio_unregister_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 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 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 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 driver. Once vfio_register_group_dev() returns it is possible for userspace to

View File

@ -627,7 +627,7 @@ static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
ret = vfio_fsl_mc_reflck_attach(vdev); ret = vfio_fsl_mc_reflck_attach(vdev);
if (ret) if (ret)
goto out_kfree; goto out_uninit;
ret = vfio_fsl_mc_init_device(vdev); ret = vfio_fsl_mc_init_device(vdev);
if (ret) if (ret)
@ -657,7 +657,8 @@ static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
vfio_fsl_uninit_device(vdev); vfio_fsl_uninit_device(vdev);
out_reflck: out_reflck:
vfio_fsl_mc_reflck_put(vdev->reflck); vfio_fsl_mc_reflck_put(vdev->reflck);
out_kfree: out_uninit:
vfio_uninit_group_dev(&vdev->vdev);
kfree(vdev); kfree(vdev);
out_group_put: out_group_put:
vfio_iommu_group_put(group, dev); 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); dprc_remove_devices(mc_dev, NULL, 0);
vfio_fsl_uninit_device(vdev); vfio_fsl_uninit_device(vdev);
vfio_fsl_mc_reflck_put(vdev->reflck); vfio_fsl_mc_reflck_put(vdev->reflck);
vfio_uninit_group_dev(&vdev->vdev);
kfree(vdev); kfree(vdev);
vfio_iommu_group_put(mc_dev->dev.iommu_group, dev); vfio_iommu_group_put(mc_dev->dev.iommu_group, dev);

View 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); vfio_init_group_dev(vdev, &mdev->dev, &vfio_mdev_dev_ops);
ret = vfio_register_group_dev(vdev); ret = vfio_register_group_dev(vdev);
if (ret) { if (ret)
kfree(vdev); goto out_uninit;
return ret;
}
dev_set_drvdata(&mdev->dev, vdev); dev_set_drvdata(&mdev->dev, vdev);
return 0; return 0;
out_uninit:
vfio_uninit_group_dev(vdev);
kfree(vdev);
return ret;
} }
static void vfio_mdev_remove(struct mdev_device *mdev) 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); struct vfio_device *vdev = dev_get_drvdata(&mdev->dev);
vfio_unregister_group_dev(vdev); vfio_unregister_group_dev(vdev);
vfio_uninit_group_dev(vdev);
kfree(vdev); kfree(vdev);
} }

View 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); ret = vfio_pci_reflck_attach(vdev);
if (ret) if (ret)
goto out_free; goto out_uninit;
ret = vfio_pci_vf_init(vdev); ret = vfio_pci_vf_init(vdev);
if (ret) if (ret)
goto out_reflck; goto out_reflck;
@ -2059,7 +2059,8 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
vfio_pci_vf_uninit(vdev); vfio_pci_vf_uninit(vdev);
out_reflck: out_reflck:
vfio_pci_reflck_put(vdev->reflck); vfio_pci_reflck_put(vdev->reflck);
out_free: out_uninit:
vfio_uninit_group_dev(&vdev->vdev);
kfree(vdev->pm_save); kfree(vdev->pm_save);
kfree(vdev); kfree(vdev);
out_group_put: out_group_put:
@ -2077,6 +2078,7 @@ static void vfio_pci_remove(struct pci_dev *pdev)
vfio_pci_vf_uninit(vdev); vfio_pci_vf_uninit(vdev);
vfio_pci_reflck_put(vdev->reflck); vfio_pci_reflck_put(vdev->reflck);
vfio_uninit_group_dev(&vdev->vdev);
vfio_pci_vga_uninit(vdev); vfio_pci_vga_uninit(vdev);
vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev); vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);

View File

@ -667,7 +667,7 @@ int vfio_platform_probe_common(struct vfio_platform_device *vdev,
ret = vfio_platform_of_probe(vdev, dev); ret = vfio_platform_of_probe(vdev, dev);
if (ret) if (ret)
return ret; goto out_uninit;
vdev->device = dev; vdev->device = dev;
@ -675,7 +675,7 @@ int vfio_platform_probe_common(struct vfio_platform_device *vdev,
if (ret && vdev->reset_required) { if (ret && vdev->reset_required) {
dev_err(dev, "No reset function found for device %s\n", dev_err(dev, "No reset function found for device %s\n",
vdev->name); vdev->name);
return ret; goto out_uninit;
} }
group = vfio_iommu_group_get(dev); group = vfio_iommu_group_get(dev);
@ -698,6 +698,8 @@ int vfio_platform_probe_common(struct vfio_platform_device *vdev,
vfio_iommu_group_put(group, dev); vfio_iommu_group_put(group, dev);
put_reset: put_reset:
vfio_platform_put_reset(vdev); vfio_platform_put_reset(vdev);
out_uninit:
vfio_uninit_group_dev(&vdev->vdev);
return ret; return ret;
} }
EXPORT_SYMBOL_GPL(vfio_platform_probe_common); 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); pm_runtime_disable(vdev->device);
vfio_platform_put_reset(vdev); vfio_platform_put_reset(vdev);
vfio_uninit_group_dev(&vdev->vdev);
vfio_iommu_group_put(vdev->vdev.dev->iommu_group, vdev->vdev.dev); vfio_iommu_group_put(vdev->vdev.dev->iommu_group, vdev->vdev.dev);
} }
EXPORT_SYMBOL_GPL(vfio_platform_remove_common); EXPORT_SYMBOL_GPL(vfio_platform_remove_common);

View File

@ -749,6 +749,11 @@ void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
} }
EXPORT_SYMBOL_GPL(vfio_init_group_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) int vfio_register_group_dev(struct vfio_device *device)
{ {
struct vfio_device *existing_device; struct vfio_device *existing_device;

View 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, void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
const struct vfio_device_ops *ops); const struct vfio_device_ops *ops);
void vfio_uninit_group_dev(struct vfio_device *device);
int vfio_register_group_dev(struct vfio_device *device); int vfio_register_group_dev(struct vfio_device *device);
void vfio_unregister_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); extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);

View File

@ -559,6 +559,7 @@ static int mbochs_probe(struct mdev_device *mdev)
dev_set_drvdata(&mdev->dev, mdev_state); dev_set_drvdata(&mdev->dev, mdev_state);
return 0; return 0;
err_mem: err_mem:
vfio_uninit_group_dev(&mdev_state->vdev);
kfree(mdev_state->pages); kfree(mdev_state->pages);
kfree(mdev_state->vconfig); kfree(mdev_state->vconfig);
kfree(mdev_state); 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); struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
vfio_unregister_group_dev(&mdev_state->vdev); vfio_unregister_group_dev(&mdev_state->vdev);
vfio_uninit_group_dev(&mdev_state->vdev);
atomic_add(mdev_state->type->mbytes, &mbochs_avail_mbytes); atomic_add(mdev_state->type->mbytes, &mbochs_avail_mbytes);
kfree(mdev_state->pages); kfree(mdev_state->pages);
kfree(mdev_state->vconfig); kfree(mdev_state->vconfig);

View File

@ -235,17 +235,16 @@ static int mdpy_probe(struct mdev_device *mdev)
mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL); mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL);
if (mdev_state->vconfig == NULL) { if (mdev_state->vconfig == NULL) {
kfree(mdev_state); ret = -ENOMEM;
return -ENOMEM; goto err_state;
} }
fbsize = roundup_pow_of_two(type->width * type->height * type->bytepp); fbsize = roundup_pow_of_two(type->width * type->height * type->bytepp);
mdev_state->memblk = vmalloc_user(fbsize); mdev_state->memblk = vmalloc_user(fbsize);
if (!mdev_state->memblk) { if (!mdev_state->memblk) {
kfree(mdev_state->vconfig); ret = -ENOMEM;
kfree(mdev_state); goto err_vconfig;
return -ENOMEM;
} }
dev_info(dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width, dev_info(dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width,
type->height); type->height);
@ -260,13 +259,18 @@ static int mdpy_probe(struct mdev_device *mdev)
mdpy_count++; mdpy_count++;
ret = vfio_register_group_dev(&mdev_state->vdev); ret = vfio_register_group_dev(&mdev_state->vdev);
if (ret) { if (ret)
kfree(mdev_state->vconfig); goto err_mem;
kfree(mdev_state);
return ret;
}
dev_set_drvdata(&mdev->dev, mdev_state); dev_set_drvdata(&mdev->dev, mdev_state);
return 0; 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) 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); vfio_unregister_group_dev(&mdev_state->vdev);
vfree(mdev_state->memblk); vfree(mdev_state->memblk);
kfree(mdev_state->vconfig); kfree(mdev_state->vconfig);
vfio_uninit_group_dev(&mdev_state->vdev);
kfree(mdev_state); kfree(mdev_state);
mdpy_count--; mdpy_count--;

View File

@ -718,8 +718,8 @@ static int mtty_probe(struct mdev_device *mdev)
mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL); mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
if (mdev_state == NULL) { if (mdev_state == NULL) {
atomic_add(nr_ports, &mdev_avail_ports); ret = -ENOMEM;
return -ENOMEM; goto err_nr_ports;
} }
vfio_init_group_dev(&mdev_state->vdev, &mdev->dev, &mtty_dev_ops); 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); mdev_state->vconfig = kzalloc(MTTY_CONFIG_SPACE_SIZE, GFP_KERNEL);
if (mdev_state->vconfig == NULL) { if (mdev_state->vconfig == NULL) {
kfree(mdev_state); ret = -ENOMEM;
atomic_add(nr_ports, &mdev_avail_ports); goto err_state;
return -ENOMEM;
} }
mutex_init(&mdev_state->ops_lock); mutex_init(&mdev_state->ops_lock);
@ -743,14 +742,19 @@ static int mtty_probe(struct mdev_device *mdev)
mtty_create_config_space(mdev_state); mtty_create_config_space(mdev_state);
ret = vfio_register_group_dev(&mdev_state->vdev); ret = vfio_register_group_dev(&mdev_state->vdev);
if (ret) { if (ret)
kfree(mdev_state); goto err_vconfig;
atomic_add(nr_ports, &mdev_avail_ports);
return ret;
}
dev_set_drvdata(&mdev->dev, mdev_state); dev_set_drvdata(&mdev->dev, mdev_state);
return 0; 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) 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); vfio_unregister_group_dev(&mdev_state->vdev);
kfree(mdev_state->vconfig); kfree(mdev_state->vconfig);
vfio_uninit_group_dev(&mdev_state->vdev);
kfree(mdev_state); kfree(mdev_state);
atomic_add(nr_ports, &mdev_avail_ports); atomic_add(nr_ports, &mdev_avail_ports);
} }