iommufd: Fail replace if device has not been attached
PATCH 1/1 iommufd: Fail replace if device has not been attached
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.
---
Hi All,
Alternative may be just allow caller to replace device even if only its alias has been attached. This can be done by a few line of codes. But it seems more reasonable to fail it. So I chose to detect and fail it. However, let me know if you have different thought.
Regards,
Yi Liu
--- drivers/iommu/iommufd/device.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
drivers/iommu/iommufd/device.c +20 -0
@@ -471,6 +471,17 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,/* The device attach/detach/replace helpers for attach_handle */ +/* Check if idev is attached to igroup->hwpt */ +static bool iommufd_device_attached (struct iommufd_device *idev) +{ +struct iommufd_device *cur; + +list_for_each_entry (cur, &idev->igroup->device_list, group_item) +if (cur == idev) +return true; +return false; +} +static int iommufd_hwpt_attach_device (struct iommufd_hw_pagetable *hwpt,struct iommufd_device *idev) {
@@ -705,6 +716,15 @@ iommufd_device_do_replace(struct iommufd_device *idev,mutex_lock (&idev->igroup->lock); +/* +* Device has not been attached yet, replace such a device is not +* supported. Fail it to prompt caller to use the correct API. +*/ +if (!iommufd_device_attached (idev)) { + rc = -EINVAL ; + goto err_unlock; + } +if (igroup->hwpt ==NULL ) { rc = -EINVAL ; goto err_unlock;
Signature · 1 line
2.34.1
Quoted 14 lines
From: Liu, Yi L <[email protected]>
Sent: Tuesday, March 4, 2025 8:08 PMThe 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.
Quoted 8 lines
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 e88d4ec154a8 ("iommufd: Add iommufd_device_replace()")Signed-off-by Yi Liu <[email protected]>
cc stable kernel
Quoted 7 lines
---
Hi All,Alternative may be just allow caller to replace device even if only its alias has been attached. This can be done by a few line of codes. But it seems more reasonable to fail it. So I chose to detect and fail it. However, let me know if you have different thought.
doing so breaks the definition of replace.
Quoted 13 lines
Regards,
Yi Liu--- drivers/iommu/iommufd/device.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)drivers/iommu/iommufd/device.c +0
-0
drivers/iommu/iommufd/device.c iommufd_device_attach_reserved_iova(struct · line 476
Quoted 6 lines
drivers/iommu/iommufd/device.c +2
-0@@ -471,6 +471,17 @@ iommufd_device_attach_reserved_iova(structiommufd_device *idev,/* The device attach/detach/replace helpers for attach_handle */ +/* Check if idev is attached to igroup->hwpt */ +static bool iommufd_device_attached(struct iommufd_device *idev)
iommufd_device_is_attached()
Quoted 14 lines
drivers/iommu/iommufd/device.c +9
-0+{ +struct iommufd_device *cur; + +list_for_each_entry (cur, &idev->igroup->device_list, group_item) +if (cur == idev) +return true; +return false; +} + static int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,drivers/iommu/iommufd/device.c +0
-0struct iommufd_device *idev) {
drivers/iommu/iommufd/device.c iommufd_device_do_replace(struct · line 723
Quoted 8 lines
drivers/iommu/iommufd/device.c +4
-0@@ -705,6 +716,15 @@ iommufd_device_do_replace(struct iommufd_device *idev,mutex_lock (&idev->igroup->lock); +/* +* Device has not been attached yet, replace such a device is not +* supported. Fail it to prompt caller to use the correct API. +*/
Just remove it. It's same meaning as what the code tells
Quoted 8 lines
drivers/iommu/iommufd/device.c +5
-0+if (!iommufd_device_attached (idev)) { + rc = -EINVAL ; + goto err_unlock; + } +if (igroup->hwpt ==NULL ) { rc = -EINVAL ;goto
err_unlock;
then this check should be a WARN_ON().
Quoted 20 lines · Kevin
Quoted 20 lines
From: Liu, Yi L <[email protected]>
Sent: Tuesday, March 4, 2025 8:08 PMThe 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.
Quoted 4 lines
Quoted 2 lines
Fixes e88d4ec154a8 ("iommufd: Add iommufd_device_replace()")Signed-off-by Yi Liu <[email protected]>cc stable kernel
sure.
Quoted 25 lines
Quoted 7 lines
---
Hi All,Alternative may be just allow caller to replace device even if only its alias has been attached. This can be done by a few line of codes. But it seems more reasonable to fail it. So I chose to detect and fail it. However, let me know if you have different thought.
doing so breaks the definition of replace.
Quoted 15 lines
Regards,
Yi Liu--- drivers/iommu/iommufd/device.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)drivers/iommu/iommufd/device.c +0
-0@@ -471,6 +471,17 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
Quoted 6 lines
drivers/iommu/iommufd/device.c iommufd_device_attach_reserved_iova(struct iommufd_device *idev, · line 475
Quoted 4 lines
drivers/iommu/iommufd/device.c +2
-0/* The device attach/detach/replace helpers for attach_handle */ +/* Check if idev is attached to igroup->hwpt */ +static bool iommufd_device_attached (struct iommufd_device *idev)
iommufd_device_is_attached()
got it.
Quoted 16 lines
Quoted 16 lines
drivers/iommu/iommufd/device.c +9
-0+{ +struct iommufd_device *cur; + +list_for_each_entry (cur, &idev->igroup->device_list, group_item) +if (cur == idev) +return true; +return false; +} + static int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,struct iommufd_device *idev) {drivers/iommu/iommufd/device.c +0
-0@@ -705,6 +716,15 @@ iommufd_device_do_replace(struct iommufd_device *idev,
Quoted 8 lines
drivers/iommu/iommufd/device.c iommufd_device_do_replace(struct iommufd_device *idev, · line 722
Quoted 6 lines
drivers/iommu/iommufd/device.c +4
-0mutex_lock (&idev->igroup->lock); +/* +* Device has not been attached yet, replace such a device is not +* supported. Fail it to prompt caller to use the correct API. +*/ Just remove it. It's same meaning as what the code tells
sure.
Quoted 10 lines
Quoted 8 lines
drivers/iommu/iommufd/device.c +5
-0+if (!iommufd_device_attached (idev)) { + rc = -EINVAL ; + goto err_unlock; + } +if (igroup->hwpt ==NULL ) { rc = -EINVAL ;goto
err_unlock;then this check should be a
WARN_ON().
you are right. let me add it.
Quoted 1 line
Reviewed-by Kevin Tian <[email protected]>
Signature · 2 lines
Regards, Yi Liu