
From: Lu Baolu <baolu.lu@linux.intel.com> mainline inclusion from mainline-v6.9-rc1 commit 0061ffe289e19caabeea8103e69cb0f1896e34d8 category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/IAHGRM Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?i... -------------------------------- The current device_release callback for individual iommu drivers does the following: 1) Silent IOMMU DMA translation: It detaches any existing domain from the device and puts it into a blocking state (some drivers might use the identity state). 2) Resource release: It releases resources allocated during the device_probe callback and restores the device to its pre-probe state. Step 1 is challenging for individual iommu drivers because each must check if a domain is already attached to the device. Additionally, if a deferred attach never occurred, the device_release should avoid modifying hardware configuration regardless of the reason for its call. To simplify this process, introduce a static release_domain within the iommu_ops structure. It can be either a blocking or identity domain depending on the iommu hardware. The iommu core will decide whether to attach this domain before the device_release callback, eliminating the need for repetitive code in various drivers. Consequently, the device_release callback can focus solely on the opposite operations of device_probe, including releasing all resources allocated during that callback. Co-developed-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Link: https://lore.kernel.org/r/20240305013305.204605-2-baolu.lu@linux.intel.com Signed-off-by: Joerg Roedel <jroedel@suse.de> Conflicts: include/linux/iommu.h [context conflict] Signed-off-by: Guo Mengqi <guomengqi3@huawei.com> --- drivers/iommu/iommu.c | 19 +++++++++++++++---- include/linux/iommu.h | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 98397cbb8b9b..e9f9e8a23006 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -466,13 +466,24 @@ static void iommu_deinit_device(struct device *dev) /* * release_device() must stop using any attached domain on the device. - * If there are still other devices in the group they are not effected + * If there are still other devices in the group, they are not affected * by this callback. * - * The IOMMU driver must set the device to either an identity or - * blocking translation and stop using any domain pointer, as it is - * going to be freed. + * If the iommu driver provides release_domain, the core code ensures + * that domain is attached prior to calling release_device. Drivers can + * use this to enforce a translation on the idle iommu. Typically, the + * global static blocked_domain is a good choice. + * + * Otherwise, the iommu driver must set the device to either an identity + * or a blocking translation in release_device() and stop using any + * domain pointer, as it is going to be freed. + * + * Regardless, if a delayed attach never occurred, then the release + * should still avoid touching any hardware configuration either. */ + if (!dev->iommu->attach_deferred && ops->release_domain) + ops->release_domain->ops->attach_dev(ops->release_domain, dev); + if (ops->release_device) ops->release_device(dev); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index f95e11c4052d..0dea4c637fd2 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -731,7 +731,7 @@ struct iommu_ops { KABI_USE(5, int (*set_group_qos_params)(struct iommu_group *group, u16 partition, u8 perf_mon_grp)) - KABI_RESERVE(6) + KABI_USE(6, struct iommu_domain *release_domain) KABI_RESERVE(7) KABI_RESERVE(8) KABI_RESERVE(9) -- 2.22.0