diff mbox series

[v4,02/10] iommufd: Refactor __fault_domain_replace_dev() to be a wrapper of iommu_replace_group_handle()

Message ID 20240912131255.13305-3-yi.l.liu@intel.com (mailing list archive)
State New
Headers show
Series iommufd support pasid attach/replace | expand

Commit Message

Yi Liu Sept. 12, 2024, 1:12 p.m. UTC
There is a wrapper of iommu_attach_group_handle(), so making a wrapper for
iommu_replace_group_handle() for further code refactor. No functional change
intended.

Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
 drivers/iommu/iommufd/fault.c | 54 ++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 23 deletions(-)

Comments

Tian, Kevin Sept. 30, 2024, 7:42 a.m. UTC | #1
> From: Liu, Yi L <yi.l.liu@intel.com>
> Sent: Thursday, September 12, 2024 9:13 PM
> 
> @@ -191,13 +187,25 @@ int iommufd_fault_domain_replace_dev(struct
> iommufd_device *idev,
>  			return ret;
>  	}
> 
> -	ret = __fault_domain_replace_dev(idev, hwpt, old);
> +	if (hwpt->fault) {
> +		curr = __fault_domain_replace_dev(idev, hwpt, old);
> +		ret = IS_ERR(curr) ? PTR_ERR(curr) : 0;
> +	} else {
> +		ret = iommu_replace_group_handle(idev->igroup->group,
> +						 hwpt->domain, NULL);
> +	}
> +
>  	if (ret) {
>  		if (iopf_on)
>  			iommufd_fault_iopf_disable(idev);
>  		return ret;
>  	}
> 
> +	if (curr) {
> +		iommufd_auto_response_faults(old, curr);
> +		kfree(curr);
> +	}
> +

this is incorrect. The old code does auto response as long as
old->fault is true and replace succeeds. But now you make
it an operation only when hwpt->fault is true.
Yi Liu Sept. 30, 2024, 10:13 a.m. UTC | #2
On 2024/9/30 15:42, Tian, Kevin wrote:
>> From: Liu, Yi L <yi.l.liu@intel.com>
>> Sent: Thursday, September 12, 2024 9:13 PM
>>
>> @@ -191,13 +187,25 @@ int iommufd_fault_domain_replace_dev(struct
>> iommufd_device *idev,
>>   			return ret;
>>   	}
>>
>> -	ret = __fault_domain_replace_dev(idev, hwpt, old);
>> +	if (hwpt->fault) {
>> +		curr = __fault_domain_replace_dev(idev, hwpt, old);
>> +		ret = IS_ERR(curr) ? PTR_ERR(curr) : 0;
>> +	} else {
>> +		ret = iommu_replace_group_handle(idev->igroup->group,
>> +						 hwpt->domain, NULL);
>> +	}
>> +
>>   	if (ret) {
>>   		if (iopf_on)
>>   			iommufd_fault_iopf_disable(idev);
>>   		return ret;
>>   	}
>>
>> +	if (curr) {
>> +		iommufd_auto_response_faults(old, curr);
>> +		kfree(curr);
>> +	}
>> +
> 
> this is incorrect. The old code does auto response as long as
> old->fault is true and replace succeeds. But now you make
> it an operation only when hwpt->fault is true.

oops, yes it is. needs to get the curr handle in this function instead of
getting it from the __fault_domain_replace_dev() helper.
diff mbox series

Patch

diff --git a/drivers/iommu/iommufd/fault.c b/drivers/iommu/iommufd/fault.c
index c4715261f2c7..cd56745f3003 100644
--- a/drivers/iommu/iommufd/fault.c
+++ b/drivers/iommu/iommufd/fault.c
@@ -146,35 +146,30 @@  void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,
 	kfree(handle);
 }
 
-static int __fault_domain_replace_dev(struct iommufd_device *idev,
-				      struct iommufd_hw_pagetable *hwpt,
-				      struct iommufd_hw_pagetable *old)
+/* Caller to free the old iommufd_attach_handle */
+static struct iommufd_attach_handle *
+__fault_domain_replace_dev(struct iommufd_device *idev,
+			   struct iommufd_hw_pagetable *hwpt,
+			   struct iommufd_hw_pagetable *old)
 {
-	struct iommufd_attach_handle *handle, *curr = NULL;
+	struct iommufd_attach_handle *handle, *curr;
 	int ret;
 
-	if (old->fault)
-		curr = iommufd_device_get_attach_handle(idev);
-
-	if (hwpt->fault) {
-		handle = kzalloc(sizeof(*handle), GFP_KERNEL);
-		if (!handle)
-			return -ENOMEM;
+	curr = iommufd_device_get_attach_handle(idev);
 
-		handle->idev = idev;
-		ret = iommu_replace_group_handle(idev->igroup->group,
-						 hwpt->domain, &handle->handle);
-	} else {
-		ret = iommu_replace_group_handle(idev->igroup->group,
-						 hwpt->domain, NULL);
-	}
+	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+	if (!handle)
+		return ERR_PTR(-ENOMEM);
 
-	if (!ret && curr) {
-		iommufd_auto_response_faults(old, curr);
-		kfree(curr);
+	handle->idev = idev;
+	ret = iommu_replace_group_handle(idev->igroup->group,
+					 hwpt->domain, &handle->handle);
+	if (ret) {
+		kfree(handle);
+		return ERR_PTR(ret);
 	}
 
-	return ret;
+	return curr;
 }
 
 int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
@@ -183,6 +178,7 @@  int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
 {
 	bool iopf_off = !hwpt->fault && old->fault;
 	bool iopf_on = hwpt->fault && !old->fault;
+	struct iommufd_attach_handle *curr = NULL;
 	int ret;
 
 	if (iopf_on) {
@@ -191,13 +187,25 @@  int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
 			return ret;
 	}
 
-	ret = __fault_domain_replace_dev(idev, hwpt, old);
+	if (hwpt->fault) {
+		curr = __fault_domain_replace_dev(idev, hwpt, old);
+		ret = IS_ERR(curr) ? PTR_ERR(curr) : 0;
+	} else {
+		ret = iommu_replace_group_handle(idev->igroup->group,
+						 hwpt->domain, NULL);
+	}
+
 	if (ret) {
 		if (iopf_on)
 			iommufd_fault_iopf_disable(idev);
 		return ret;
 	}
 
+	if (curr) {
+		iommufd_auto_response_faults(old, curr);
+		kfree(curr);
+	}
+
 	if (iopf_off)
 		iommufd_fault_iopf_disable(idev);