[PATCH OLK-6.6] iommufd: Rollback IOPF and refcount when iopf/sva enabling fails
hulk inclusion category: bugfix bugzilla: https://gitcode.com/openeuler/kernel/issues/10441 ------------------ iommufd_fault_iopf_enable() has three problems on the failure paths, all introduced by commit 6b45626422f7 ("iommufd: enable SVA when IOPF enabled"): 1. When IOMMU_DEV_FEAT_SVA enabling fails, the function returns without unlocking idev->iopf_lock, leaving the mutex held forever and deadlocking any later enable()/disable(). 2. That same path 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. 3. 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 all three 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, which also silences the unlock-missing.cocci warning. Fixes: 6b45626422f7 ("iommufd: enable SVA when IOPF enabled") Signed-off-by: Zhang Yuwei <zhangyuwei20@huawei.com> --- drivers/iommu/iommufd/fault.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommufd/fault.c b/drivers/iommu/iommufd/fault.c index 6ae29981fe38..66015cb5e4da 100644 --- a/drivers/iommu/iommufd/fault.c +++ b/drivers/iommu/iommufd/fault.c @@ -44,12 +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); + ret = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA); if (ret) - return 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
反馈: 您发送到kernel@openeuler.org的补丁/补丁集,已成功转换为PR! PR链接地址: https://atomgit.com/openeuler/kernel/merge_requests/31576 邮件列表地址:https://mailweb.openeuler.org/archives/list/kernel@openeuler.org/message/KXF... FeedBack: The patch(es) which you have sent to kernel@openeuler.org mailing list has been converted to a pull request successfully! Pull request link: https://atomgit.com/openeuler/kernel/merge_requests/31576 Mailing list address: https://mailweb.openeuler.org/archives/list/kernel@openeuler.org/message/KXF...
participants (2)
-
patchwork bot -
Zhang Yuwei