Re: [PATCH 16/24] iommu/amd: Introduce IOMMUFD vDevice support for AMD
From: Suthikulpanit, Suravee
Date: Wed Sep 02 2026 - 22:42:30 EST
On 8/19/2026 8:34 PM, guanghuifeng@xxxxxxxxxxxxxxxxx wrote:
在 2026/7/27 21:29, Suravee Suthikulpanit 写道:
Initialize vDevice for AMD vIOMMU by setting up the Device ID Mapping
table using the guest device ID.
Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@xxxxxxx>
---
drivers/iommu/amd/amd_iommu_types.h | 10 ++++++++++
drivers/iommu/amd/iommufd.c | 31 +++++++++++++++++++++++++++++
drivers/iommu/amd/nested.c | 27 ++++++++++++++++++++++---
3 files changed, 65 insertions(+), 3 deletions(-)
....
diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c
index bfc4b0ec22a9..1975b2932107 100644
--- a/drivers/iommu/amd/iommufd.c
+++ b/drivers/iommu/amd/iommufd.c
@@ -9,6 +9,7 @@
#include "amd_iommu.h"
#include "amd_viommu.h"
#include "amd_iommu_types.h"
+#include "../iommufd/iommufd_private.h"
static const struct iommufd_viommu_ops amd_viommu_ops;
@@ -126,6 +127,34 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)
amd_iommu_gid_free(iommu, aviommu->gid);
}
+/*
+ * Called from drivers/iommu/iommufd/viommu.c: iommufd_vdevice_alloc_ioctl()
+ */
+static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev)
+{
+ struct iommu_dev_data *dev_data;
+ struct pci_dev *pdev = to_pci_dev(vdev->idev->dev);
+ struct iommufd_viommu *viommu = vdev->viommu;
+ struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core);
+
+ if (!pdev) {
+ pr_err("%s: not a PCI device\n", __func__);
+ return -EINVAL;
+ }
+
to_pci_dev() is a container_of(), so it can never return NULL and the
!pdev check is dead code. Worse, for a non-PCI device it yields a bogus
pointer which is then dereferenced through dev_iommu_priv_get() below.
I will fix the code to check dev_is_pci() properly.
Thanks,
Suravee