Skip to content

Commit 91be0bd

Browse files
jgunthorpeAlex Williamson
authored andcommitted
vfio/pci: Have all VFIO PCI drivers store the vfio_pci_core_device in drvdata
Having a consistent pointer in the drvdata will allow the next patch to make use of the drvdata from some of the core code helpers. Use a WARN_ON inside vfio_pci_core_register_device() to detect drivers that miss this. Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/1-v4-c841817a0349+8f-vfio_get_from_dev_jgg@nvidia.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent eadd86f commit 91be0bd

4 files changed

Lines changed: 27 additions & 9 deletions

File tree

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,14 @@ static int vf_qm_cache_wb(struct hisi_qm *qm)
337337
return 0;
338338
}
339339

340+
static struct hisi_acc_vf_core_device *hssi_acc_drvdata(struct pci_dev *pdev)
341+
{
342+
struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev->dev);
343+
344+
return container_of(core_device, struct hisi_acc_vf_core_device,
345+
core_device);
346+
}
347+
340348
static void vf_qm_fun_reset(struct hisi_acc_vf_core_device *hisi_acc_vdev,
341349
struct hisi_qm *qm)
342350
{
@@ -962,7 +970,7 @@ hisi_acc_vfio_pci_get_device_state(struct vfio_device *vdev,
962970

963971
static void hisi_acc_vf_pci_aer_reset_done(struct pci_dev *pdev)
964972
{
965-
struct hisi_acc_vf_core_device *hisi_acc_vdev = dev_get_drvdata(&pdev->dev);
973+
struct hisi_acc_vf_core_device *hisi_acc_vdev = hssi_acc_drvdata(pdev);
966974

967975
if (hisi_acc_vdev->core_device.vdev.migration_flags !=
968976
VFIO_MIGRATION_STOP_COPY)
@@ -1274,11 +1282,10 @@ static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device
12741282
&hisi_acc_vfio_pci_ops);
12751283
}
12761284

1285+
dev_set_drvdata(&pdev->dev, &hisi_acc_vdev->core_device);
12771286
ret = vfio_pci_core_register_device(&hisi_acc_vdev->core_device);
12781287
if (ret)
12791288
goto out_free;
1280-
1281-
dev_set_drvdata(&pdev->dev, hisi_acc_vdev);
12821289
return 0;
12831290

12841291
out_free:
@@ -1289,7 +1296,7 @@ static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device
12891296

12901297
static void hisi_acc_vfio_pci_remove(struct pci_dev *pdev)
12911298
{
1292-
struct hisi_acc_vf_core_device *hisi_acc_vdev = dev_get_drvdata(&pdev->dev);
1299+
struct hisi_acc_vf_core_device *hisi_acc_vdev = hssi_acc_drvdata(pdev);
12931300

12941301
vfio_pci_core_unregister_device(&hisi_acc_vdev->core_device);
12951302
vfio_pci_core_uninit_device(&hisi_acc_vdev->core_device);

drivers/vfio/pci/mlx5/main.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
/* Arbitrary to prevent userspace from consuming endless memory */
2525
#define MAX_MIGRATION_SIZE (512*1024*1024)
2626

27+
static struct mlx5vf_pci_core_device *mlx5vf_drvdata(struct pci_dev *pdev)
28+
{
29+
struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev->dev);
30+
31+
return container_of(core_device, struct mlx5vf_pci_core_device,
32+
core_device);
33+
}
34+
2735
static struct page *
2836
mlx5vf_get_migration_page(struct mlx5_vf_migration_file *migf,
2937
unsigned long offset)
@@ -518,7 +526,7 @@ static int mlx5vf_pci_get_device_state(struct vfio_device *vdev,
518526

519527
static void mlx5vf_pci_aer_reset_done(struct pci_dev *pdev)
520528
{
521-
struct mlx5vf_pci_core_device *mvdev = dev_get_drvdata(&pdev->dev);
529+
struct mlx5vf_pci_core_device *mvdev = mlx5vf_drvdata(pdev);
522530

523531
if (!mvdev->migrate_cap)
524532
return;
@@ -592,11 +600,10 @@ static int mlx5vf_pci_probe(struct pci_dev *pdev,
592600
return -ENOMEM;
593601
vfio_pci_core_init_device(&mvdev->core_device, pdev, &mlx5vf_pci_ops);
594602
mlx5vf_cmd_set_migratable(mvdev);
603+
dev_set_drvdata(&pdev->dev, &mvdev->core_device);
595604
ret = vfio_pci_core_register_device(&mvdev->core_device);
596605
if (ret)
597606
goto out_free;
598-
599-
dev_set_drvdata(&pdev->dev, mvdev);
600607
return 0;
601608

602609
out_free:
@@ -608,7 +615,7 @@ static int mlx5vf_pci_probe(struct pci_dev *pdev,
608615

609616
static void mlx5vf_pci_remove(struct pci_dev *pdev)
610617
{
611-
struct mlx5vf_pci_core_device *mvdev = dev_get_drvdata(&pdev->dev);
618+
struct mlx5vf_pci_core_device *mvdev = mlx5vf_drvdata(pdev);
612619

613620
vfio_pci_core_unregister_device(&mvdev->core_device);
614621
mlx5vf_cmd_remove_migratable(mvdev);

drivers/vfio/pci/vfio_pci.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
151151
return -ENOMEM;
152152
vfio_pci_core_init_device(vdev, pdev, &vfio_pci_ops);
153153

154+
dev_set_drvdata(&pdev->dev, vdev);
154155
ret = vfio_pci_core_register_device(vdev);
155156
if (ret)
156157
goto out_free;
157-
dev_set_drvdata(&pdev->dev, vdev);
158158
return 0;
159159

160160
out_free:

drivers/vfio/pci/vfio_pci_core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,10 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)
18211821
struct pci_dev *pdev = vdev->pdev;
18221822
int ret;
18231823

1824+
/* Drivers must set the vfio_pci_core_device to their drvdata */
1825+
if (WARN_ON(vdev != dev_get_drvdata(&vdev->pdev->dev)))
1826+
return -EINVAL;
1827+
18241828
if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
18251829
return -EINVAL;
18261830

0 commit comments

Comments
 (0)