vfio/pci: Remove vfio_device_get_from_dev()
Subsystems (5)
Jason Gunthorpe
reviewerPATCH v2 1/1 vfio/pci: Remove vfio_device_get_from_dev()
The last user of this function is in PCI callbacks that want to convert their struct pci_dev to a vfio_device. Instead of searching use the vfio_device available trivially through the drvdata.
When a callback in the device_driver is called, the caller must hold the device_lock() on dev. The purpose of the device_lock is to prevent remove() from being called (see __device_release_driver), and allow the driver to safely interact with its drvdata without races.
The PCI core correctly follows this and holds the device_lock() when calling error_detected (see report_error_detected) and sriov_configure (see sriov_numvfs_store).
Further, since the drvdata holds a positive refcount on the vfio_device any access of the drvdata, under the driver_lock, from a driver callback needs no further protection or refcounting.
Thus the remark in the vfio_device_get_from_dev() comment does not apply here, VFIO PCI drivers all call vfio_unregister_group_dev() from their remove callbacks under the driver lock and cannot race with the remaining callers.
--- .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 12 +++++- drivers/vfio/pci/mlx5/main.c | 10 ++++- drivers/vfio/pci/vfio_pci.c | 18 +++++++- drivers/vfio/pci/vfio_pci_core.c | 42 ++++--------------- drivers/vfio/vfio.c | 41 +----------------- include/linux/vfio.h | 2 - include/linux/vfio_pci_core.h | 9 ++-- 7 files changed, 50 insertions(+), 84 deletions(-)
v2:
- Rebase on the vfio_mdev_no_group branch, which is on top of gvt & iommu
- Delete vfio_group_get_from_dev() as well due to the rebase
v1: https://lore.kernel.org/r/[email protected]
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +11 -1
@@ -1305,9 +1305,19 @@ static const struct pci_device_id hisi_acc_vfio_pci_table[] = {MODULE_DEVICE_TABLE (pci, hisi_acc_vfio_pci_table); +static pci_ers_result_t hisi_acc_aer_err_detected (struct pci_dev *pdev, +pci_channel_state_t state) +{ +struct hisi_acc_vf_core_device *hisi_acc_vdev = +dev_get_drvdata (&pdev->dev); + +return vfio_pci_core_aer_err_detected (&hisi_acc_vdev->core_device, + state); +} +static const struct pci_error_handlers hisi_acc_vf_err_handlers = { .reset_done = hisi_acc_vf_pci_aer_reset_done, - .error_detected = vfio_pci_core_aer_err_detected, + .error_detected = hisi_acc_aer_err_detected, };static struct pci_driver hisi_acc_vfio_pci_driver = {
drivers/vfio/pci/mlx5/main.c +9 -1
@@ -643,9 +643,17 @@ static const struct pci_device_id mlx5vf_pci_table[] = {MODULE_DEVICE_TABLE (pci, mlx5vf_pci_table); +static pci_ers_result_t mlx5vf_pci_aer_err_detected (struct pci_dev *pdev, +pci_channel_state_t state) +{ +struct mlx5vf_pci_core_device *mvdev =dev_get_drvdata (&pdev->dev); + +return vfio_pci_core_aer_err_detected (&mvdev->core_device, state); +} +static const struct pci_error_handlers mlx5vf_err_handlers = { .reset_done = mlx5vf_pci_aer_reset_done, - .error_detected = vfio_pci_core_aer_err_detected, + .error_detected = mlx5vf_pci_aer_err_detected, };static struct pci_driver mlx5vf_pci_driver = {
drivers/vfio/pci/vfio_pci.c +16 -2
@@ -174,10 +174,12 @@ static void vfio_pci_remove(struct pci_dev *pdev)static int vfio_pci_sriov_configure (struct pci_dev *pdev,int nr_virtfn) { +struct vfio_pci_core_device *vdev =dev_get_drvdata (&pdev->dev); +if (!enable_sriov)return -ENOENT ; -return vfio_pci_core_sriov_configure (pdev, nr_virtfn); +return vfio_pci_core_sriov_configure (vdev, nr_virtfn); }static const struct pci_device_id vfio_pci_table[] = {
@@ -187,13 +189,25 @@ static const struct pci_device_id vfio_pci_table[] = {MODULE_DEVICE_TABLE (pci, vfio_pci_table); +static pci_ers_result_t vfio_pci_aer_err_detected (struct pci_dev *pdev, +pci_channel_state_t state) +{ +struct vfio_pci_core_device *vdev =dev_get_drvdata (&pdev->dev); + +return vfio_pci_core_aer_err_detected (vdev, state); +} + +static const struct pci_error_handlers vfio_pci_err_handlers = { + .error_detected = vfio_pci_aer_err_detected, +}; +static struct pci_driver vfio_pci_driver = { .name ="vfio-pci" , .id_table = vfio_pci_table, .probe = vfio_pci_probe, .remove = vfio_pci_remove, .sriov_configure = vfio_pci_sriov_configure, - .err_handler = &vfio_pci_core_err_handlers, + .err_handler = &vfio_pci_err_handlers, .driver_managed_dma = true, };
drivers/vfio/pci/vfio_pci_core.c +8 -34
86 lines not on this page · raw message
drivers/vfio/vfio.c +1 -40
61 lines not on this page · raw message
include/linux/vfio.h +0 -2
@@ -125,8 +125,6 @@ void vfio_uninit_group_dev(struct vfio_device *device);int vfio_register_group_dev (struct vfio_device *device);int vfio_register_emulated_iommu_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 void vfio_device_put (struct vfio_device *device);int vfio_assign_device_set (struct vfio_device *device,void *set_id);
include/linux/vfio_pci_core.h +5 -4
@@ -227,8 +227,8 @@ void vfio_pci_core_init_device(struct vfio_pci_core_device *vdev,int vfio_pci_core_register_device (struct vfio_pci_core_device *vdev);void vfio_pci_core_uninit_device (struct vfio_pci_core_device *vdev);void vfio_pci_core_unregister_device (struct vfio_pci_core_device *vdev); -int vfio_pci_core_sriov_configure (struct pci_dev *pdev,int nr_virtfn); -extern const struct pci_error_handlers vfio_pci_core_err_handlers; +int vfio_pci_core_sriov_configure (struct vfio_pci_core_device *vdev, +int nr_virtfn);long vfio_pci_core_ioctl (struct vfio_device *core_vdev,unsigned int cmd,unsigned long arg); int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
@@ -243,8 +243,9 @@ int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);int vfio_pci_core_enable (struct vfio_pci_core_device *vdev);void vfio_pci_core_disable (struct vfio_pci_core_device *vdev);void vfio_pci_core_finish_enable (struct vfio_pci_core_device *vdev); -pci_ers_result_t vfio_pci_core_aer_err_detected (struct pci_dev *pdev, -pci_channel_state_t state); +pci_ers_result_t +vfio_pci_core_aer_err_detected (struct vfio_pci_core_device *vdev, +pci_channel_state_t state);static inline bool vfio_pci_is_vga (struct pci_dev *pdev) {
base-commit: 21ce7c292b5684f930cc63f3841ec692bbd6c10a
Signature · 1 line
2.36.0
Quoted 17 lines · Jason Gunthorpe
The last user of this function is in PCI callbacks that want to convert their struct
pci_devto avfio_device. Instead of searching use thevfio_deviceavailable trivially through the drvdata.When a callback in the
device_driveris called, the caller must hold thedevice_lock()on dev. The purpose of thedevice_lockis to preventremove()from being called (see__device_release_driver), and allow the driver to safely interact with its drvdata without races.The PCI core correctly follows this and holds the
device_lock()when callingerror_detected(seereport_error_detected) andsriov_configure(seesriov_numvfs_store).Further, since the drvdata holds a positive refcount on the
vfio_deviceany access of the drvdata, under thedriver_lock, from a driver callback needs no further protection or refcounting.
Quoted 7 lines
Thus the remark in the
vfio_device_get_from_dev()comment does not apply here, VFIO PCI drivers all callvfio_unregister_group_dev()from their remove callbacks under the driver lock and cannot race with the remaining callers.Reviewed-by Kevin Tian <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>
Quoted 22 lines
-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: 03 May 2022 00:42
To: Alex Williamson <[email protected]>; Cornelia Huck <[email protected]>; [email protected]; liulongfang <[email protected]>; Shameerali Kolothum Thodi <[email protected]>; Yishai Hadas <[email protected]>
Cc: Kevin Tian <[email protected]>
Subject: [PATCH v2] vfio/pci: Removevfio_device_get_from_dev()The last user of this function is in PCI callbacks that want to convert their struct
pci_devto avfio_device. Instead of searching use thevfio_deviceavailable trivially through the drvdata.When a callback in the
device_driveris called, the caller must hold thedevice_lock()on dev. The purpose of thedevice_lockis to preventremove()from being called (see__device_release_driver), and allow the driver to safely interact with its drvdata without races.
Quoted 6 lines
The PCI core correctly follows this and holds the
device_lock()when callingerror_detected(seereport_error_detected) andsriov_configure(seesriov_numvfs_store).Further, since the drvdata holds a positive refcount on the
vfio_deviceany access of the drvdata, under thedriver_lock, from a driver callback
device_lock() ? (v1 discussion says it's a typo).
Quoted 6 lines
needs no further protection or refcounting.
Thus the remark in the
vfio_device_get_from_dev()comment does not apply here, VFIO PCI drivers all callvfio_unregister_group_dev()from their remove callbacks under the driver lock and cannot race with the remaining callers.
May be we can also mention the removal of vfio_group_get_from_dev() as well in the commit.
Quoted 2 lines
Reviewed-by Kevin Tian <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>
Thanks,
Shameer
Jason Gunthorpe
reviewerQuoted 4 lines · Shameerali Kolothum Thodi
Quoted 2 lines
Further, since the drvdata holds a positive refcount on the
vfio_deviceany access of the drvdata, under thedriver_lock, from a driver callback
device_lock()? (v1 discussion says it's a typo).
Yes, I fixed it
Quoted 9 lines
Quoted 6 lines
needs no further protection or refcounting.
Thus the remark in the
vfio_device_get_from_dev()comment does not apply here, VFIO PCI drivers all callvfio_unregister_group_dev()from their remove callbacks under the driver lock and cannot race with the remaining callers.May be we can also mention the removal of
vfio_group_get_from_dev()as well in the commit.
It is in the subject :)
Thanks,
Jason