vfio/pci: Give drivers control of PCI driver_data
Subsystems (11)
v3 cover vfio/pci: Give drivers control of PCI driver_data
Hi all,
This patch lets VFIO PCI variant drivers store their private structures in PCI driver_data, removing the requirement that it point directly to struct vfio_pci_core_device. This also accommodates the typed driver data used by the Rust PCI infrastructure, as discussed for the proposed Rust NVIDIA vGPU VFIO driver [1].
Runtime PM and AER helpers receive an explicit vfio_pci_core_device pointer. VFIO_PCI_CORE_DEFINE_CALLBACKS() generates per-driver wrappers that retrieve the driver's private data and pass its embedded core device to these helpers. Each driver installs its own PM ops table.
VGA initialization selects the decode callback in vfio_pci_vga_init() based on vfio_vga_disabled(vdev). Both callbacks use pci_dev directly and do not depend on driver_data.
Changes since v2 [2]:
- Drop the unused pci_channel_state_t argument from the core AER helper and its callers, preserving the PCI callback signature (Danilo).
- Wrap each driver's PM ops pointer with pm_ptr() so unused tables can be removed when CONFIG_PM=n (Sashiko).
Changes since v1:
- Extend VFIO_PCI_CORE_DEFINE_CALLBACKS() to accept the driver's private structure type and the member containing vfio_pci_core_device.
- Store each variant driver's private structure in PCI driver_data.
- Select the VGA decode callback in the core and retain the one-argument vfio_pci_core_register_device() interface.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/r/[email protected]/
Thanks,
Simon
Simon Song (1):
vfio/pci: Remove the core dependency on driver data
.../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 27 ++++---- drivers/vfio/pci/ism/main.c | 19 +++--- drivers/vfio/pci/mlx5/main.c | 13 ++-- drivers/vfio/pci/nvgrace-gpu/main.c | 19 +++--- drivers/vfio/pci/pds/pci_drv.c | 8 ++- drivers/vfio/pci/pds/vfio_dev.c | 5 +- drivers/vfio/pci/qat/main.c | 12 ++-- drivers/vfio/pci/vfio_pci.c | 33 +++++++++- drivers/vfio/pci/vfio_pci_core.c | 61 +++++++------------ drivers/vfio/pci/virtio/main.c | 8 ++- drivers/vfio/pci/xe/main.c | 8 ++- include/linux/vfio_pci_core.h | 41 ++++++++++++- 12 files changed, 159 insertions(+), 95 deletions(-)
Signature · 1 line
2.43.7
PATCH v3 1/1 vfio/pci: Remove the core dependency on driver data
vfio-pci-core currently has runtime functions that interpret pci driver_data as a pointer to vfio_pci_core_device, and enforce vfio variant drivers must set vfio_pci_core_device to their pci driver_data. This constrains variant drivers' private-data layout, including the typed driver data used by the Rust PCI infrastructure.
Add VFIO_PCI_CORE_DEFINE_CALLBACKS marcos to generate wrapper code for each vfio variant driver, update vfio variant drivers to store their private strctures in driver_data and use generated wrapper to pass the vfio core device to helpers.
Select the vga decode callback during vfio_pci_vga_init based on if vfio_vga_disabled, the callback only use pci_dev and does not require driver_data
--- .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 27 ++++---- drivers/vfio/pci/ism/main.c | 19 +++--- drivers/vfio/pci/mlx5/main.c | 13 ++-- drivers/vfio/pci/nvgrace-gpu/main.c | 19 +++--- drivers/vfio/pci/pds/pci_drv.c | 8 ++- drivers/vfio/pci/pds/vfio_dev.c | 5 +- drivers/vfio/pci/qat/main.c | 12 ++-- drivers/vfio/pci/vfio_pci.c | 33 +++++++++- drivers/vfio/pci/vfio_pci_core.c | 61 +++++++------------ drivers/vfio/pci/virtio/main.c | 8 ++- drivers/vfio/pci/xe/main.c | 8 ++- include/linux/vfio_pci_core.h | 41 ++++++++++++- 12 files changed, 159 insertions(+), 95 deletions(-)
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +12 -15
@@ -16,6 +16,9 @@#include "hisi_acc_vfio_pci.h" +VFIO_PCI_CORE_DEFINE_CALLBACKS(hisi_acc_vf, struct hisi_acc_vf_core_device, + core_device) + /* Return 0 on VM acc device ready, -ETIMEDOUT hardware timeout */static int qm_wait_dev_not_ready (struct hisi_qm *qm) {
@@ -588,10 +591,7 @@ static int vf_qm_state_save(struct hisi_acc_vf_core_device *hisi_acc_vdev,static struct hisi_acc_vf_core_device *hisi_acc_drvdata (struct pci_dev *pdev) { -struct vfio_pci_core_device *core_device =dev_get_drvdata (&pdev->dev); - - return container_of(core_device, struct hisi_acc_vf_core_device, - core_device); +return pci_get_drvdata (pdev); }/* Check the PF's RAS state and Function INT state */
@@ -1416,9 +1416,8 @@ static int hisi_acc_vf_debug_check(struct seq_file *seq, struct vfio_device *vdestatic int hisi_acc_vf_debug_cmd (struct seq_file *seq,void *data) {struct device *vf_dev = seq->private; -struct vfio_pci_core_device *core_device =dev_get_drvdata (vf_dev); -struct vfio_device *vdev = &core_device->vdev; -struct hisi_acc_vf_core_device *hisi_acc_vdev =hisi_acc_get_vf_dev (vdev); +struct hisi_acc_vf_core_device *hisi_acc_vdev =dev_get_drvdata (vf_dev); +struct vfio_device *vdev = &hisi_acc_vdev->core_device.vdev;struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;u64 value;int ret;
@@ -1445,9 +1444,8 @@ static int hisi_acc_vf_debug_cmd(struct seq_file *seq, void *data)static int hisi_acc_vf_dev_read (struct seq_file *seq,void *data) {struct device *vf_dev = seq->private; -struct vfio_pci_core_device *core_device =dev_get_drvdata (vf_dev); -struct vfio_device *vdev = &core_device->vdev; -struct hisi_acc_vf_core_device *hisi_acc_vdev =hisi_acc_get_vf_dev (vdev); +struct hisi_acc_vf_core_device *hisi_acc_vdev =dev_get_drvdata (vf_dev); +struct vfio_device *vdev = &hisi_acc_vdev->core_device.vdev; size_t vf_data_sz = offsetofend(struct acc_vf_data, padding);struct acc_vf_data *vf_data;int ret;
@@ -1492,9 +1490,7 @@ static int hisi_acc_vf_dev_read(struct seq_file *seq, void *data)static int hisi_acc_vf_migf_read (struct seq_file *seq,void *data) {struct device *vf_dev = seq->private; -struct vfio_pci_core_device *core_device =dev_get_drvdata (vf_dev); -struct vfio_device *vdev = &core_device->vdev; -struct hisi_acc_vf_core_device *hisi_acc_vdev =hisi_acc_get_vf_dev (vdev); +struct hisi_acc_vf_core_device *hisi_acc_vdev =dev_get_drvdata (vf_dev); size_t vf_data_sz = offsetofend(struct acc_vf_data, padding);struct hisi_acc_vf_migration_file *debug_migf = hisi_acc_vdev->debug_migf;
@@ -1687,7 +1683,7 @@ static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_deviceif (IS_ERR (hisi_acc_vdev))return PTR_ERR (hisi_acc_vdev); -dev_set_drvdata (&pdev->dev, &hisi_acc_vdev->core_device); +dev_set_drvdata (&pdev->dev, hisi_acc_vdev); ret =vfio_pci_core_register_device (&hisi_acc_vdev->core_device);if (ret) goto out_put_vdev;
@@ -1721,7 +1717,7 @@ MODULE_DEVICE_TABLE(pci, hisi_acc_vfio_pci_table);static const struct pci_error_handlers hisi_acc_vf_err_handlers = { .reset_prepare = hisi_acc_vf_pci_reset_prepare, .reset_done = hisi_acc_vf_pci_aer_reset_done, - .error_detected = vfio_pci_core_aer_err_detected, + .error_detected = hisi_acc_vf_aer_err_detected, };static struct pci_driver hisi_acc_vfio_pci_driver = {
@@ -1729,6 +1725,7 @@ static struct pci_driver hisi_acc_vfio_pci_driver = { .id_table = hisi_acc_vfio_pci_table, .probe = hisi_acc_vfio_pci_probe, .remove = hisi_acc_vfio_pci_remove, + .driver = { .pm = pm_ptr(&hisi_acc_vf_pm_ops) }, .err_handler = &hisi_acc_vf_err_handlers, .driver_managed_dma = true, };
drivers/vfio/pci/ism/main.c +11 -8
@@ -47,6 +47,9 @@ struct ism_vfio_pci_core_device {struct kmem_cache *store_block_cache; }; +VFIO_PCI_CORE_DEFINE_CALLBACKS(ism_vfio, struct ism_vfio_pci_core_device, + core_device) +static int ism_vfio_pci_open_device (struct vfio_device *core_vdev) {struct ism_vfio_pci_core_device *ivpcd;
@@ -363,7 +366,7 @@ static int ism_vfio_pci_probe(struct pci_dev *pdev,if (IS_ERR (ivpcd))return PTR_ERR (ivpcd); -dev_set_drvdata (&pdev->dev, &ivpcd->core_device); +dev_set_drvdata (&pdev->dev, ivpcd); ret =vfio_pci_core_register_device (&ivpcd->core_device);if (ret)
@@ -374,12 +377,7 @@ static int ism_vfio_pci_probe(struct pci_dev *pdev,static void ism_vfio_pci_remove (struct pci_dev *pdev) { -struct vfio_pci_core_device *core_device; -struct ism_vfio_pci_core_device *ivpcd; - - core_device =dev_get_drvdata (&pdev->dev); - ivpcd = container_of(core_device, struct ism_vfio_pci_core_device, - core_device); +struct ism_vfio_pci_core_device *ivpcd =pci_get_drvdata (pdev);vfio_pci_core_unregister_device (&ivpcd->core_device);vfio_put_device (&ivpcd->core_device.vdev);
@@ -392,12 +390,17 @@ static const struct pci_device_id ism_device_table[] = { };MODULE_DEVICE_TABLE (pci, ism_device_table); +static const struct pci_error_handlers ism_vfio_err_handlers = { + .error_detected = ism_vfio_aer_err_detected, +}; +static struct pci_driver ism_vfio_pci_driver = { .name =KBUILD_MODNAME , .id_table = ism_device_table, .probe = ism_vfio_pci_probe, .remove = ism_vfio_pci_remove, - .err_handler = &vfio_pci_core_err_handlers, + .driver = { .pm =pm_ptr (&ism_vfio_pm_ops) }, + .err_handler = &ism_vfio_err_handlers, .driver_managed_dma = true, };
drivers/vfio/pci/mlx5/main.c +7 -6
@@ -21,6 +21,9 @@#include "cmd.h" +VFIO_PCI_CORE_DEFINE_CALLBACKS(mlx5vf, struct mlx5vf_pci_core_device, + core_device) + /* Device specification max LOAD size */#define MAX_LOAD_SIZE (BIT_ULL(__mlx5_bit_sz(load_vhca_state_in, size)) - 1)
@@ -28,10 +31,7 @@static struct mlx5vf_pci_core_device *mlx5vf_drvdata (struct pci_dev *pdev) { -struct vfio_pci_core_device *core_device =dev_get_drvdata (&pdev->dev); - - return container_of(core_device, struct mlx5vf_pci_core_device, - core_device); +return pci_get_drvdata (pdev); }static void mlx5vf_disable_fd (struct mlx5_vf_migration_file *migf)
@@ -1415,7 +1415,7 @@ static int mlx5vf_pci_probe(struct pci_dev *pdev,if (IS_ERR (mvdev))return PTR_ERR (mvdev); -dev_set_drvdata (&pdev->dev, &mvdev->core_device); +dev_set_drvdata (&pdev->dev, mvdev); ret =vfio_pci_core_register_device (&mvdev->core_device);if (ret) goto out_put_vdev;
@@ -1443,7 +1443,7 @@ MODULE_DEVICE_TABLE(pci, mlx5vf_pci_table);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_aer_err_detected, };static struct pci_driver mlx5vf_pci_driver = {
@@ -1451,6 +1451,7 @@ static struct pci_driver mlx5vf_pci_driver = { .id_table = mlx5vf_pci_table, .probe = mlx5vf_pci_probe, .remove = mlx5vf_pci_remove, + .driver = { .pm = pm_ptr(&mlx5vf_pm_ops) }, .err_handler = &mlx5vf_err_handlers, .driver_managed_dma = true, };
drivers/vfio/pci/nvgrace-gpu/main.c +10 -9
@@ -72,6 +72,9 @@ struct nvgrace_gpu_pci_core_device {int cxl_dvsec; }; +VFIO_PCI_CORE_DEFINE_CALLBACKS(nvgrace_gpu, struct nvgrace_gpu_pci_core_device, + core_device) +static void nvgrace_gpu_init_fake_bar_emu_regs (struct vfio_device *core_vdev) { struct nvgrace_gpu_pci_core_device *nvdev =
@@ -1340,7 +1343,7 @@ static int nvgrace_gpu_probe(struct pci_dev *pdev,if (ret) goto out_put_vdev; -dev_set_drvdata (&pdev->dev, &nvdev->core_device); +dev_set_drvdata (&pdev->dev, nvdev);if (ops == &nvgrace_gpu_pci_ops) { nvdev->has_mig_hw_bug =nvgrace_gpu_has_mig_hw_bug (pdev);
@@ -1371,10 +1374,10 @@ static int nvgrace_gpu_probe(struct pci_dev *pdev,static void nvgrace_gpu_remove (struct pci_dev *pdev) { -struct vfio_pci_core_device *core_device =dev_get_drvdata (&pdev->dev); +struct nvgrace_gpu_pci_core_device *nvdev =pci_get_drvdata (pdev); -vfio_pci_core_unregister_device (core_device); -vfio_put_device (&core_device->vdev); +vfio_pci_core_unregister_device (&nvdev->core_device); +vfio_put_device (&nvdev->core_device.vdev); }static const struct pci_device_id nvgrace_gpu_vfio_pci_table[] = {
@@ -1406,17 +1409,14 @@ MODULE_DEVICE_TABLE(pci, nvgrace_gpu_vfio_pci_table);*/ static void nvgrace_gpu_vfio_pci_reset_done (struct pci_dev *pdev) { -struct vfio_pci_core_device *core_device =dev_get_drvdata (&pdev->dev); -struct nvgrace_gpu_pci_core_device *nvdev = - container_of(core_device, struct nvgrace_gpu_pci_core_device, - core_device); +struct nvgrace_gpu_pci_core_device *nvdev =pci_get_drvdata (pdev); nvdev->reset_done = true; }static const struct pci_error_handlers nvgrace_gpu_vfio_pci_err_handlers = { .reset_done = nvgrace_gpu_vfio_pci_reset_done, - .error_detected = vfio_pci_core_aer_err_detected, + .error_detected = nvgrace_gpu_aer_err_detected, };static struct pci_driver nvgrace_gpu_vfio_pci_driver = {
@@ -1424,6 +1424,7 @@ static struct pci_driver nvgrace_gpu_vfio_pci_driver = { .id_table = nvgrace_gpu_vfio_pci_table, .probe = nvgrace_gpu_probe, .remove = nvgrace_gpu_remove, + .driver = { .pm = pm_ptr(&nvgrace_gpu_pm_ops) }, .err_handler = &nvgrace_gpu_vfio_pci_err_handlers, .driver_managed_dma = true, };
drivers/vfio/pci/pds/pci_drv.c +6 -2
@@ -16,6 +16,9 @@#include "pci_drv.h" #include "cmds.h" +VFIO_PCI_CORE_DEFINE_CALLBACKS(pds_vfio, struct pds_vfio_pci_device, + vfio_coredev) +#define PDS_VFIO_DRV_DESCRIPTION "AMD/Pensando VFIO Device Driver"#define PCI_VENDOR_ID_PENSANDO 0x1dd8
@@ -118,7 +121,7 @@ static int pds_vfio_pci_probe(struct pci_dev *pdev,if (IS_ERR (pds_vfio))return PTR_ERR (pds_vfio); -dev_set_drvdata (&pdev->dev, &pds_vfio->vfio_coredev); +dev_set_drvdata (&pdev->dev, pds_vfio); err =vfio_pci_core_register_device (&pds_vfio->vfio_coredev);if (err)
@@ -173,7 +176,7 @@ static void pds_vfio_pci_aer_reset_done(struct pci_dev *pdev)static const struct pci_error_handlers pds_vfio_pci_err_handlers = { .reset_done = pds_vfio_pci_aer_reset_done, - .error_detected = vfio_pci_core_aer_err_detected, + .error_detected = pds_vfio_aer_err_detected, };static struct pci_driver pds_vfio_pci_driver = {
@@ -181,6 +184,7 @@ static struct pci_driver pds_vfio_pci_driver = { .id_table = pds_vfio_pci_table, .probe = pds_vfio_pci_probe, .remove = pds_vfio_pci_remove, + .driver = { .pm = pm_ptr(&pds_vfio_pm_ops) }, .err_handler = &pds_vfio_pci_err_handlers, .driver_managed_dma = true, };
drivers/vfio/pci/pds/vfio_dev.c +1 -4
@@ -20,10 +20,7 @@ struct device *pds_vfio_to_dev(struct pds_vfio_pci_device *pds_vfio)struct pds_vfio_pci_device *pds_vfio_pci_drvdata (struct pci_dev *pdev) { -struct vfio_pci_core_device *core_device =dev_get_drvdata (&pdev->dev); - - return container_of(core_device, struct pds_vfio_pci_device, - vfio_coredev); +return pci_get_drvdata (pdev); } void pds_vfio_reset(struct pds_vfio_pci_device *pds_vfio,
drivers/vfio/pci/qat/main.c +7 -5
@@ -55,6 +55,9 @@ struct qat_vf_core_device {struct qat_vf_migration_file *saving_migf; }; +VFIO_PCI_CORE_DEFINE_CALLBACKS(qat_vf, struct qat_vf_core_device, + core_device) +static int qat_vf_pci_open_device (struct vfio_device *core_vdev) { struct qat_vf_core_device *qat_vdev =
@@ -623,9 +626,7 @@ static const struct vfio_device_ops qat_vf_pci_ops = {static struct qat_vf_core_device *qat_vf_drvdata (struct pci_dev *pdev) { -struct vfio_pci_core_device *core_device =pci_get_drvdata (pdev); - - return container_of(core_device, struct qat_vf_core_device, core_device); +return pci_get_drvdata (pdev); }static void qat_vf_pci_aer_reset_done (struct pci_dev *pdev)
@@ -651,7 +652,7 @@ qat_vf_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (IS_ERR (qat_vdev))return PTR_ERR (qat_vdev); -pci_set_drvdata (pdev, &qat_vdev->core_device); +pci_set_drvdata (pdev, qat_vdev); ret =vfio_pci_core_register_device (&qat_vdev->core_device);if (ret) goto out_put_device;
@@ -686,7 +687,7 @@ MODULE_DEVICE_TABLE(pci, qat_vf_vfio_pci_table);static const struct pci_error_handlers qat_vf_err_handlers = { .reset_done = qat_vf_pci_aer_reset_done, - .error_detected = vfio_pci_core_aer_err_detected, + .error_detected = qat_vf_aer_err_detected, };static struct pci_driver qat_vf_vfio_pci_driver = {
@@ -694,6 +695,7 @@ static struct pci_driver qat_vf_vfio_pci_driver = { .id_table = qat_vf_vfio_pci_table, .probe = qat_vf_vfio_pci_probe, .remove = qat_vf_vfio_pci_remove, + .driver = { .pm = pm_ptr(&qat_vf_pm_ops) }, .err_handler = &qat_vf_err_handlers, .driver_managed_dma = true, };
drivers/vfio/pci/vfio_pci.c +32 -1
@@ -223,13 +223,44 @@ static const struct pci_device_id vfio_pci_table[] = {MODULE_DEVICE_TABLE (pci, vfio_pci_table); +static int __maybe_unused vfio_pci_pm_suspend (struct device *dev) +{ +struct vfio_pci_core_device *vdev =dev_get_drvdata (dev); + +return vfio_pci_core_runtime_suspend (vdev); +} + +static int __maybe_unused vfio_pci_pm_resume (struct device *dev) +{ +struct vfio_pci_core_device *vdev =dev_get_drvdata (dev); + +return vfio_pci_core_runtime_resume (vdev); +} + +static const struct dev_pm_ops vfio_pci_pm_ops = { +SET_RUNTIME_PM_OPS (vfio_pci_pm_suspend, vfio_pci_pm_resume,NULL ) +}; + +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 =pci_get_drvdata (pdev); + +return vfio_pci_core_aer_err_detected (vdev); +} + +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 = { .pm =pm_ptr (&vfio_pci_pm_ops) }, .driver_managed_dma = true, };
drivers/vfio/pci/vfio_pci_core.c +23 -38
148 lines not on this page · raw message
drivers/vfio/pci/virtio/main.c +6 -2
@@ -18,6 +18,9 @@#include "common.h" +VFIO_PCI_CORE_DEFINE_CALLBACKS(virtiovf, struct virtiovf_pci_core_device, + core_device) +static int virtiovf_pci_open_device (struct vfio_device *core_vdev) { struct virtiovf_pci_core_device *virtvdev = container_of(core_vdev,
@@ -174,7 +177,7 @@ static int virtiovf_pci_probe(struct pci_dev *pdev,if (sup_lm)virtiovf_set_migratable (virtvdev); -dev_set_drvdata (&pdev->dev, &virtvdev->core_device); +dev_set_drvdata (&pdev->dev, virtvdev); ret =vfio_pci_core_register_device (&virtvdev->core_device);if (ret) goto out;
@@ -211,7 +214,7 @@ static void virtiovf_pci_aer_reset_done(struct pci_dev *pdev)static const struct pci_error_handlers virtiovf_err_handlers = { .reset_done = virtiovf_pci_aer_reset_done, - .error_detected = vfio_pci_core_aer_err_detected, + .error_detected = virtiovf_aer_err_detected, };static struct pci_driver virtiovf_pci_driver = {
@@ -219,6 +222,7 @@ static struct pci_driver virtiovf_pci_driver = { .id_table = virtiovf_pci_table, .probe = virtiovf_pci_probe, .remove = virtiovf_pci_remove, + .driver = { .pm = pm_ptr(&virtiovf_pm_ops) }, .err_handler = &virtiovf_err_handlers, .driver_managed_dma = true, };
drivers/vfio/pci/xe/main.c +6 -2
@@ -38,6 +38,9 @@ struct xe_vfio_pci_core_device {struct xe_vfio_pci_migration_file *migf; }; +VFIO_PCI_CORE_DEFINE_CALLBACKS(xe_vfio, struct xe_vfio_pci_core_device, + core_device) +#define xe_vdev_to_dev (xe_vdev) (&(xe_vdev)->core_device.pdev->dev)static void xe_vfio_pci_disable_file (struct xe_vfio_pci_migration_file *migf)
@@ -140,7 +143,7 @@ static void xe_vfio_pci_reset_done(struct pci_dev *pdev)static const struct pci_error_handlers xe_vfio_pci_err_handlers = { .reset_prepare = xe_vfio_pci_reset_prepare, .reset_done = xe_vfio_pci_reset_done, - .error_detected = vfio_pci_core_aer_err_detected, + .error_detected = xe_vfio_aer_err_detected, };static int xe_vfio_pci_open_device (struct vfio_device *core_vdev)
@@ -550,7 +553,7 @@ static int xe_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *iif (IS_ERR (xe_vdev))return PTR_ERR (xe_vdev); -dev_set_drvdata (&pdev->dev, &xe_vdev->core_device); +dev_set_drvdata (&pdev->dev, xe_vdev); ret =vfio_pci_core_register_device (&xe_vdev->core_device);if (ret) {
@@ -586,6 +589,7 @@ static struct pci_driver xe_vfio_pci_driver = { .id_table = xe_vfio_pci_table, .probe = xe_vfio_pci_probe, .remove = xe_vfio_pci_remove, + .driver = { .pm = pm_ptr(&xe_vfio_pm_ops) }, .err_handler = &xe_vfio_pci_err_handlers, .driver_managed_dma = true, };
include/linux/vfio_pci_core.h +38 -3
69 lines not on this page · raw message
Signature · 1 line
2.43.7
Re: [PATCH v3 1/1] vfio/pci: Remove the core dependency on driver data
Quoted 15 lines · Simon Song
vfio-pci-core currently has runtime functions that interpret pci
driver_dataas a pointer tovfio_pci_core_device, and enforce vfio variant drivers must setvfio_pci_core_deviceto their pcidriver_data. This constrains variant drivers' private-data layout, including the typed driver data used by the Rust PCI infrastructure.Add
VFIO_PCI_CORE_DEFINE_CALLBACKSmarcos to generate wrapper code for each vfio variant driver, update vfio variant drivers to store their private strctures indriver_dataand use generated wrapper to pass the vfio core device to helpers.Select the vga decode callback during
vfio_pci_vga_initbased on ifvfio_vga_disabled, the callback only usepci_devand does not requiredriver_data
Quoted 6 lines
Assisted-by LLMSuggested-by Jason Gunthorpe <[email protected]>Suggested-by Alex Williamson <[email protected]>Suggested-by Danilo Krummrich <[email protected]>Signed-off-by Simon Song <[email protected]>
Re: [PATCH v3 1/1] vfio/pci: Remove the core dependency on driver data
In reply to Simon SongQuoted 21 lines · Simon Song
vfio-pci-core currently has runtime functions that interpret pci
driver_dataas a pointer tovfio_pci_core_device, and enforce vfio variant drivers must setvfio_pci_core_deviceto their pcidriver_data. This constrains variant drivers' private-data layout, including the typed driver data used by the Rust PCI infrastructure.Add
VFIO_PCI_CORE_DEFINE_CALLBACKSmarcos to generate wrapper code for each vfio variant driver, update vfio variant drivers to store their private strctures indriver_dataand use generated wrapper to pass the vfio core device to helpers.Select the vga decode callback during
vfio_pci_vga_initbased on ifvfio_vga_disabled, the callback only usepci_devand does not requiredriver_dataAssisted-by LLMSuggested-by Jason Gunthorpe <[email protected]>Suggested-by Alex Williamson <[email protected]>Suggested-by Danilo Krummrich <[email protected]>Signed-off-by Simon Song <[email protected]>
Quoted 14 lines
--- .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 27 ++++---- drivers/vfio/pci/ism/main.c | 19 +++--- drivers/vfio/pci/mlx5/main.c | 13 ++-- drivers/vfio/pci/nvgrace-gpu/main.c | 19 +++--- drivers/vfio/pci/pds/pci_drv.c | 8 ++- drivers/vfio/pci/pds/vfio_dev.c | 5 +- drivers/vfio/pci/qat/main.c | 12 ++-- drivers/vfio/pci/vfio_pci.c | 33 +++++++++- drivers/vfio/pci/vfio_pci_core.c | 61 +++++++------------ drivers/vfio/pci/virtio/main.c | 8 ++- drivers/vfio/pci/xe/main.c | 8 ++- include/linux/vfio_pci_core.h | 41 ++++++++++++- 12 files changed, 159 insertions(+), 95 deletions(-)
This looks OK to me, but it probably should have been 3 patches, mixing the VGA stuff into all of this (and you have a smart approach) is not how upstream likes to see things arranged.
Jason
Alex Williamson
maintainerRe: [PATCH v3 1/1] vfio/pci: Remove the core dependency on driver data
Quoted 37 lines · On Fri, 9 Oct 2026 16:28:00 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 21 lines · Simon Song
vfio-pci-core currently has runtime functions that interpret pci
driver_dataas a pointer tovfio_pci_core_device, and enforce vfio variant drivers must setvfio_pci_core_deviceto their pcidriver_data. This constrains variant drivers' private-data layout, including the typed driver data used by the Rust PCI infrastructure.Add
VFIO_PCI_CORE_DEFINE_CALLBACKSmarcos to generate wrapper code for each vfio variant driver, update vfio variant drivers to store their private strctures indriver_dataand use generated wrapper to pass the vfio core device to helpers.Select the vga decode callback during
vfio_pci_vga_initbased on ifvfio_vga_disabled, the callback only usepci_devand does not requiredriver_dataAssisted-by LLMSuggested-by Jason Gunthorpe <[email protected]>Suggested-by Alex Williamson <[email protected]>Suggested-by Danilo Krummrich <[email protected]>Signed-off-by Simon Song <[email protected]>Quoted 14 lines
--- .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 27 ++++---- drivers/vfio/pci/ism/main.c | 19 +++--- drivers/vfio/pci/mlx5/main.c | 13 ++-- drivers/vfio/pci/nvgrace-gpu/main.c | 19 +++--- drivers/vfio/pci/pds/pci_drv.c | 8 ++- drivers/vfio/pci/pds/vfio_dev.c | 5 +- drivers/vfio/pci/qat/main.c | 12 ++-- drivers/vfio/pci/vfio_pci.c | 33 +++++++++- drivers/vfio/pci/vfio_pci_core.c | 61 +++++++------------ drivers/vfio/pci/virtio/main.c | 8 ++- drivers/vfio/pci/xe/main.c | 8 ++- include/linux/vfio_pci_core.h | 41 ++++++++++++- 12 files changed, 159 insertions(+), 95 deletions(-)
Quoted 3 lines
This looks OK to me, but it probably should have been 3 patches, mixing the VGA stuff into all of this (and you have a smart approach) is not how upstream likes to see things arranged.
The VGA stuff actually goes away entirely when this is rebased onto Dave's vgaarb changes in linux-next[1]. All 3 VGA related chunks are simply dropped. vga_client_register() takes an opaque pointer, which then already removes the dependency on the pci_dev drvdata.
To apply, the below 3 chunks are dropped in their entirety. If this looks right, please someone double check, I'll apply it. Thanks,
Alex
[1]https://lore.kernel.org/all/[email protected]/
drivers/vfio/pci/vfio_pci_core.c +18 -7
@@ -153,6 +153,12 @@ static inline void vfio_pci_core_debugfs_init(struct vfio_pci_core_device *vdev) }#endif /* CONFIG_VFIO_DEBUGFS */ +static unsigned int vfio_pci_vga_decode_all (struct pci_dev *pdev,bool single_vga) +{ +return VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM | +VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +} +/* * Our VGA arbiter participation is limited since we don't know anything * about the device itself. However, if the device is the only VGA device
@@ -161,16 +167,14 @@ static inline void vfio_pci_core_debugfs_init(struct vfio_pci_core_device *vdev)* has no way to get to it and routing can be disabled externally at the * bridge. */ -static unsigned int vfio_pci_set_decode (struct pci_dev *pdev,bool single_vga) +static unsigned int vfio_pci_vga_decode_novga (struct pci_dev *pdev,bool single_vga) { -struct vfio_pci_core_device *vdev =dev_get_drvdata (&pdev->dev);struct pci_dev *tmp =NULL ;unsigned char max_busnr;unsigned int decodes; -if (single_vga || !vfio_vga_disabled (vdev) ||pci_is_root_bus (pdev->bus)) -return VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM | -VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +if (single_vga ||pci_is_root_bus (pdev->bus)) +return vfio_pci_vga_decode_all (pdev, single_vga); max_busnr =pci_bus_max_busnr (pdev->bus); decodes =VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM ;
@@ -2148,20 +2138,27 @@ static void vfio_pci_vf_uninit(struct vfio_pci_core_device *vdev)static int vfio_pci_vga_init (struct vfio_pci_core_device *vdev) { +unsigned int (*set_decode)(struct pci_dev *pdev,bool single_vga);struct pci_dev *pdev = vdev->pdev;int ret;if (!vfio_pci_is_vga (pdev))return 0 ; +/* VGA access is fixed for the lifetime of this registration. */ +if (vfio_vga_disabled (vdev)) + set_decode = vfio_pci_vga_decode_novga; +else + set_decode = vfio_pci_vga_decode_all; + ret =aperture_remove_conflicting_pci_devices (pdev, vdev->vdev.ops->name);if (ret)return ret; - ret =vga_client_register (pdev, vfio_pci_set_decode); + ret =vga_client_register (pdev, set_decode);if (ret)return ret; -vga_set_legacy_decoding (pdev,vfio_pci_set_decode (pdev, false)); +vga_set_legacy_decoding (pdev,set_decode (pdev, false));return 0 ; }
Re: [PATCH v3 1/1] vfio/pci: Remove the core dependency on driver data
Quoted 2 lines · Alex Williamson
To apply, the below 3 chunks are dropped in their entirety. If this looks right, please someone double check, I'll apply it.
Looks correct to me.
Thanks,
Danilo
Quoted 39 lines · On Mon, 5 Oct 2026 11:42:35 -0700 Simon Song <[email protected]> wrote:
Hi all,
This patch lets VFIO PCI variant drivers store their private structures in PCI
driver_data, removing the requirement that it point directly to structvfio_pci_core_device. This also accommodates the typed driver data used by the Rust PCI infrastructure, as discussed for the proposed Rust NVIDIA vGPU VFIO driver [1].Runtime PM and AER helpers receive an explicit
vfio_pci_core_devicepointer.VFIO_PCI_CORE_DEFINE_CALLBACKS()generates per-driver wrappers that retrieve the driver's private data and pass its embedded core device to these helpers. Each driver installs its own PM ops table.VGA initialization selects the decode callback in
vfio_pci_vga_init()based onvfio_vga_disabled(vdev). Both callbacks usepci_devdirectly and do not depend ondriver_data.Changes since v2 [2]:
- Drop the unusedpci_channel_state_targument from the core AER helper and its callers, preserving the PCI callback signature (Danilo).
- Wrap each driver's PM ops pointer withpm_ptr()so unused tables can be removed whenCONFIG_PM=n(Sashiko).Changes since v1:
- ExtendVFIO_PCI_CORE_DEFINE_CALLBACKS()to accept the driver's private structure type and the member containingvfio_pci_core_device.
- Store each variant driver's private structure in PCIdriver_data.
- Select the VGA decode callback in the core and retain the one-argumentvfio_pci_core_register_device()interface.[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/r/[email protected]/Thanks,
SimonSimon Song (1):
vfio/pci: Remove the core dependency on driver data
Quoted 14 lines
.../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 27 ++++---- drivers/vfio/pci/ism/main.c | 19 +++--- drivers/vfio/pci/mlx5/main.c | 13 ++-- drivers/vfio/pci/nvgrace-gpu/main.c | 19 +++--- drivers/vfio/pci/pds/pci_drv.c | 8 ++- drivers/vfio/pci/pds/vfio_dev.c | 5 +- drivers/vfio/pci/qat/main.c | 12 ++-- drivers/vfio/pci/vfio_pci.c | 33 +++++++++- drivers/vfio/pci/vfio_pci_core.c | 61 +++++++------------ drivers/vfio/pci/virtio/main.c | 8 ++- drivers/vfio/pci/xe/main.c | 8 ++- include/linux/vfio_pci_core.h | 41 ++++++++++++- 12 files changed, 159 insertions(+), 95 deletions(-)
I pulled in Dave's topic/pci-vgaarb-rework-2026-10-01, which gives us 93d41424caf7. The vfio_pci_core_device is now registered with the vga client so we no longer need to derive anything from the pci_dev. This makes all the VGA changes here unnecessary.
Applied to vfio next branch for v7.4 with the VGA chunks (shown in the discussion thread) and commit log paragraph describing them dropped. Thanks,
Alex
Quoted 11 lines · Alex Williamson
I pulled in Dave's topic/pci-vgaarb-rework-2026-10-01, which gives us
93d41424caf7. Thevfio_pci_core_deviceis now registered with the vga client so we no longer need to derive anything from thepci_dev. This makes all the VGA changes here unnecessary.Applied to vfio next branch for v7.4 with the VGA chunks (shown in the discussion thread) and commit log paragraph describing them dropped. Thanks,
Alex
Hi Alex,
Thanks for applying this and handling the rebase, dropping the VGA change looks good with Dave's change.
Thanks also to Jason and Danilo for the reviews.
Simon