Offering: HULK hulk inclusion category: bugfix bugzilla: https://gitcode.com/openeuler/kernel/issues/10441 ------------------ iommufd_fault_iopf_enable() has two state-consistency problems on the failure paths, both introduced by commit 6b45626422f7 ("iommufd: enable SVA when IOPF enabled"): 1. When IOMMU_DEV_FEAT_SVA enabling fails, the function returns without disabling the already-enabled IOMMU_DEV_FEAT_IOPF and without undoing the incremented idev->iopf_enabled. The leftover refcount makes the next enable() hit the "already enabled" shortcut and return success while SVA was never actually enabled, so the device is attached to a fault domain with SVA silently missing. 2. When IOMMU_DEV_FEAT_IOPF enabling fails, the error is not propagated: the code keeps going and enables SVA, overwriting the return value. If SVA happens to succeed, the function returns 0 although IOPF is off and the refcount is back to 0, and the later disable() path hits WARN_ON(iopf_enabled == 0). Fix both by jumping to a common undo path when IOPF enabling fails, rolling back IOPF when SVA enabling fails, and dropping the refcount on any failure, so that a failed enable() leaves the device exactly as it was found, which is the contract its callers rely on. The unlock also converges back to a single exit, superseding the previous partial fix. Fixes: 6b45626422f7 ("iommufd: enable SVA when IOPF enabled") Signed-off-by: Zhang Yuwei <zhangyuwei20@huawei.com> --- drivers/iommu/iommufd/fault.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/iommufd/fault.c b/drivers/iommu/iommufd/fault.c index 5935c44c58e2..66015cb5e4da 100644 --- a/drivers/iommu/iommufd/fault.c +++ b/drivers/iommu/iommufd/fault.c @@ -44,14 +44,15 @@ static int iommufd_fault_iopf_enable(struct iommufd_device *idev) ret = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_IOPF); if (ret) - --idev->iopf_enabled; + goto out_undo; - ret = iommu_dev_enable_feature(idev->dev, IOMMU_DEV_FEAT_SVA); - if (ret) { - mutex_unlock(&idev->iopf_lock); - return ret; - } + ret = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA); + if (ret) + iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_IOPF); +out_undo: + if (ret) + --idev->iopf_enabled; mutex_unlock(&idev->iopf_lock); return ret; -- 2.22.0