Skip to content

Commit 5d9484c

Browse files
yiliu1765gregkh
authored andcommitted
iommufd: Fail replace if device has not been attached
commit 55c85fa upstream. The current implementation of iommufd_device_do_replace() implicitly assumes that the input device has already been attached. However, there is no explicit check to verify this assumption. If another device within the same group has been attached, the replace operation might succeed, but the input device itself may not have been attached yet. As a result, the input device might not be tracked in the igroup->device_list, and its reserved IOVA might not be added. Despite this, the caller might incorrectly assume that the device has been successfully replaced, which could lead to unexpected behavior or errors. To address this issue, add a check to ensure that the input device has been attached before proceeding with the replace operation. This check will help maintain the integrity of the device tracking system and prevent potential issues arising from incorrect assumptions about the device's attachment status. Fixes: e88d4ec ("iommufd: Add iommufd_device_replace()") Link: https://patch.msgid.link/r/[email protected] Cc: [email protected] Reviewed-by: Kevin Tian <[email protected]> Signed-off-by: Yi Liu <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 6d11543 commit 5d9484c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

drivers/iommu/iommufd/device.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,17 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
354354

355355
/* The device attach/detach/replace helpers for attach_handle */
356356

357+
/* Check if idev is attached to igroup->hwpt */
358+
static bool iommufd_device_is_attached(struct iommufd_device *idev)
359+
{
360+
struct iommufd_device *cur;
361+
362+
list_for_each_entry(cur, &idev->igroup->device_list, group_item)
363+
if (cur == idev)
364+
return true;
365+
return false;
366+
}
367+
357368
static int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,
358369
struct iommufd_device *idev)
359370
{
@@ -593,6 +604,11 @@ iommufd_device_do_replace(struct iommufd_device *idev,
593604
goto err_unlock;
594605
}
595606

607+
if (!iommufd_device_is_attached(idev)) {
608+
rc = -EINVAL;
609+
goto err_unlock;
610+
}
611+
596612
if (hwpt == igroup->hwpt) {
597613
mutex_unlock(&idev->igroup->lock);
598614
return NULL;

0 commit comments

Comments
 (0)