Skip to content

Commit ae03c37

Browse files
mgurtovoyAlex Williamson
authored andcommitted
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>
1 parent de5494a commit ae03c37

10 files changed

Lines changed: 64 additions & 33 deletions

File tree

Documentation/driver-api/vfio.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,13 @@ vfio_unregister_group_dev() respectively::
255255
void vfio_init_group_dev(struct vfio_device *device,
256256
struct device *dev,
257257
const struct vfio_device_ops *ops);
258+
void vfio_uninit_group_dev(struct vfio_device *device);
258259
int vfio_register_group_dev(struct vfio_device *device);
259260
void vfio_unregister_group_dev(struct vfio_device *device);
260261

261262
The driver should embed the vfio_device in its own structure and call
262-
vfio_init_group_dev() to pre-configure it before going to registration.
263+
vfio_init_group_dev() to pre-configure it before going to registration
264+
and call vfio_uninit_group_dev() after completing the un-registration.
263265
vfio_register_group_dev() indicates to the core to begin tracking the
264266
iommu_group of the specified dev and register the dev as owned by a VFIO bus
265267
driver. Once vfio_register_group_dev() returns it is possible for userspace to

drivers/vfio/fsl-mc/vfio_fsl_mc.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
627627

628628
ret = vfio_fsl_mc_reflck_attach(vdev);
629629
if (ret)
630-
goto out_kfree;
630+
goto out_uninit;
631631

632632
ret = vfio_fsl_mc_init_device(vdev);
633633
if (ret)
@@ -657,7 +657,8 @@ static int vfio_fsl_mc_probe(struct fsl_mc_device *mc_dev)
657657
vfio_fsl_uninit_device(vdev);
658658
out_reflck:
659659
vfio_fsl_mc_reflck_put(vdev->reflck);
660-
out_kfree:
660+
out_uninit:
661+
vfio_uninit_group_dev(&vdev->vdev);
661662
kfree(vdev);
662663
out_group_put:
663664
vfio_iommu_group_put(group, dev);
@@ -675,7 +676,7 @@ static int vfio_fsl_mc_remove(struct fsl_mc_device *mc_dev)
675676
dprc_remove_devices(mc_dev, NULL, 0);
676677
vfio_fsl_uninit_device(vdev);
677678
vfio_fsl_mc_reflck_put(vdev->reflck);
678-
679+
vfio_uninit_group_dev(&vdev->vdev);
679680
kfree(vdev);
680681
vfio_iommu_group_put(mc_dev->dev.iommu_group, dev);
681682

drivers/vfio/mdev/vfio_mdev.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,24 @@ static int vfio_mdev_probe(struct mdev_device *mdev)
120120

121121
vfio_init_group_dev(vdev, &mdev->dev, &vfio_mdev_dev_ops);
122122
ret = vfio_register_group_dev(vdev);
123-
if (ret) {
124-
kfree(vdev);
125-
return ret;
126-
}
123+
if (ret)
124+
goto out_uninit;
125+
127126
dev_set_drvdata(&mdev->dev, vdev);
128127
return 0;
128+
129+
out_uninit:
130+
vfio_uninit_group_dev(vdev);
131+
kfree(vdev);
132+
return ret;
129133
}
130134

131135
static void vfio_mdev_remove(struct mdev_device *mdev)
132136
{
133137
struct vfio_device *vdev = dev_get_drvdata(&mdev->dev);
134138

135139
vfio_unregister_group_dev(vdev);
140+
vfio_uninit_group_dev(vdev);
136141
kfree(vdev);
137142
}
138143

drivers/vfio/pci/vfio_pci.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
20222022

20232023
ret = vfio_pci_reflck_attach(vdev);
20242024
if (ret)
2025-
goto out_free;
2025+
goto out_uninit;
20262026
ret = vfio_pci_vf_init(vdev);
20272027
if (ret)
20282028
goto out_reflck;
@@ -2059,7 +2059,8 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
20592059
vfio_pci_vf_uninit(vdev);
20602060
out_reflck:
20612061
vfio_pci_reflck_put(vdev->reflck);
2062-
out_free:
2062+
out_uninit:
2063+
vfio_uninit_group_dev(&vdev->vdev);
20632064
kfree(vdev->pm_save);
20642065
kfree(vdev);
20652066
out_group_put:
@@ -2077,6 +2078,7 @@ static void vfio_pci_remove(struct pci_dev *pdev)
20772078

20782079
vfio_pci_vf_uninit(vdev);
20792080
vfio_pci_reflck_put(vdev->reflck);
2081+
vfio_uninit_group_dev(&vdev->vdev);
20802082
vfio_pci_vga_uninit(vdev);
20812083

20822084
vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);

drivers/vfio/platform/vfio_platform_common.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,15 +667,15 @@ int vfio_platform_probe_common(struct vfio_platform_device *vdev,
667667
ret = vfio_platform_of_probe(vdev, dev);
668668

669669
if (ret)
670-
return ret;
670+
goto out_uninit;
671671

672672
vdev->device = dev;
673673

674674
ret = vfio_platform_get_reset(vdev);
675675
if (ret && vdev->reset_required) {
676676
dev_err(dev, "No reset function found for device %s\n",
677677
vdev->name);
678-
return ret;
678+
goto out_uninit;
679679
}
680680

681681
group = vfio_iommu_group_get(dev);
@@ -698,6 +698,8 @@ int vfio_platform_probe_common(struct vfio_platform_device *vdev,
698698
vfio_iommu_group_put(group, dev);
699699
put_reset:
700700
vfio_platform_put_reset(vdev);
701+
out_uninit:
702+
vfio_uninit_group_dev(&vdev->vdev);
701703
return ret;
702704
}
703705
EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
@@ -708,6 +710,7 @@ void vfio_platform_remove_common(struct vfio_platform_device *vdev)
708710

709711
pm_runtime_disable(vdev->device);
710712
vfio_platform_put_reset(vdev);
713+
vfio_uninit_group_dev(&vdev->vdev);
711714
vfio_iommu_group_put(vdev->vdev.dev->iommu_group, vdev->vdev.dev);
712715
}
713716
EXPORT_SYMBOL_GPL(vfio_platform_remove_common);

drivers/vfio/vfio.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,11 @@ void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
749749
}
750750
EXPORT_SYMBOL_GPL(vfio_init_group_dev);
751751

752+
void vfio_uninit_group_dev(struct vfio_device *device)
753+
{
754+
}
755+
EXPORT_SYMBOL_GPL(vfio_uninit_group_dev);
756+
752757
int vfio_register_group_dev(struct vfio_device *device)
753758
{
754759
struct vfio_device *existing_device;

include/linux/vfio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
6161

6262
void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
6363
const struct vfio_device_ops *ops);
64+
void vfio_uninit_group_dev(struct vfio_device *device);
6465
int vfio_register_group_dev(struct vfio_device *device);
6566
void vfio_unregister_group_dev(struct vfio_device *device);
6667
extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);

samples/vfio-mdev/mbochs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ static int mbochs_probe(struct mdev_device *mdev)
559559
dev_set_drvdata(&mdev->dev, mdev_state);
560560
return 0;
561561
err_mem:
562+
vfio_uninit_group_dev(&mdev_state->vdev);
562563
kfree(mdev_state->pages);
563564
kfree(mdev_state->vconfig);
564565
kfree(mdev_state);
@@ -572,6 +573,7 @@ static void mbochs_remove(struct mdev_device *mdev)
572573
struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
573574

574575
vfio_unregister_group_dev(&mdev_state->vdev);
576+
vfio_uninit_group_dev(&mdev_state->vdev);
575577
atomic_add(mdev_state->type->mbytes, &mbochs_avail_mbytes);
576578
kfree(mdev_state->pages);
577579
kfree(mdev_state->vconfig);

samples/vfio-mdev/mdpy.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,16 @@ static int mdpy_probe(struct mdev_device *mdev)
235235

236236
mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL);
237237
if (mdev_state->vconfig == NULL) {
238-
kfree(mdev_state);
239-
return -ENOMEM;
238+
ret = -ENOMEM;
239+
goto err_state;
240240
}
241241

242242
fbsize = roundup_pow_of_two(type->width * type->height * type->bytepp);
243243

244244
mdev_state->memblk = vmalloc_user(fbsize);
245245
if (!mdev_state->memblk) {
246-
kfree(mdev_state->vconfig);
247-
kfree(mdev_state);
248-
return -ENOMEM;
246+
ret = -ENOMEM;
247+
goto err_vconfig;
249248
}
250249
dev_info(dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width,
251250
type->height);
@@ -260,13 +259,18 @@ static int mdpy_probe(struct mdev_device *mdev)
260259
mdpy_count++;
261260

262261
ret = vfio_register_group_dev(&mdev_state->vdev);
263-
if (ret) {
264-
kfree(mdev_state->vconfig);
265-
kfree(mdev_state);
266-
return ret;
267-
}
262+
if (ret)
263+
goto err_mem;
268264
dev_set_drvdata(&mdev->dev, mdev_state);
269265
return 0;
266+
err_mem:
267+
vfree(mdev_state->memblk);
268+
err_vconfig:
269+
kfree(mdev_state->vconfig);
270+
err_state:
271+
vfio_uninit_group_dev(&mdev_state->vdev);
272+
kfree(mdev_state);
273+
return ret;
270274
}
271275

272276
static void mdpy_remove(struct mdev_device *mdev)
@@ -278,6 +282,7 @@ static void mdpy_remove(struct mdev_device *mdev)
278282
vfio_unregister_group_dev(&mdev_state->vdev);
279283
vfree(mdev_state->memblk);
280284
kfree(mdev_state->vconfig);
285+
vfio_uninit_group_dev(&mdev_state->vdev);
281286
kfree(mdev_state);
282287

283288
mdpy_count--;

samples/vfio-mdev/mtty.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,8 @@ static int mtty_probe(struct mdev_device *mdev)
718718

719719
mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
720720
if (mdev_state == NULL) {
721-
atomic_add(nr_ports, &mdev_avail_ports);
722-
return -ENOMEM;
721+
ret = -ENOMEM;
722+
goto err_nr_ports;
723723
}
724724

725725
vfio_init_group_dev(&mdev_state->vdev, &mdev->dev, &mtty_dev_ops);
@@ -732,9 +732,8 @@ static int mtty_probe(struct mdev_device *mdev)
732732
mdev_state->vconfig = kzalloc(MTTY_CONFIG_SPACE_SIZE, GFP_KERNEL);
733733

734734
if (mdev_state->vconfig == NULL) {
735-
kfree(mdev_state);
736-
atomic_add(nr_ports, &mdev_avail_ports);
737-
return -ENOMEM;
735+
ret = -ENOMEM;
736+
goto err_state;
738737
}
739738

740739
mutex_init(&mdev_state->ops_lock);
@@ -743,14 +742,19 @@ static int mtty_probe(struct mdev_device *mdev)
743742
mtty_create_config_space(mdev_state);
744743

745744
ret = vfio_register_group_dev(&mdev_state->vdev);
746-
if (ret) {
747-
kfree(mdev_state);
748-
atomic_add(nr_ports, &mdev_avail_ports);
749-
return ret;
750-
}
751-
745+
if (ret)
746+
goto err_vconfig;
752747
dev_set_drvdata(&mdev->dev, mdev_state);
753748
return 0;
749+
750+
err_vconfig:
751+
kfree(mdev_state->vconfig);
752+
err_state:
753+
vfio_uninit_group_dev(&mdev_state->vdev);
754+
kfree(mdev_state);
755+
err_nr_ports:
756+
atomic_add(nr_ports, &mdev_avail_ports);
757+
return ret;
754758
}
755759

756760
static void mtty_remove(struct mdev_device *mdev)
@@ -761,6 +765,7 @@ static void mtty_remove(struct mdev_device *mdev)
761765
vfio_unregister_group_dev(&mdev_state->vdev);
762766

763767
kfree(mdev_state->vconfig);
768+
vfio_uninit_group_dev(&mdev_state->vdev);
764769
kfree(mdev_state);
765770
atomic_add(nr_ports, &mdev_avail_ports);
766771
}

0 commit comments

Comments
 (0)