[1/2] iommu/vt-d: Remove the dead code in init_iommu_hw()

Message ID 20230530092503.152926-2-yanfei.xu@intel.com
State New
Headers
Series Misc cleanup for iommu/vt-d |

Commit Message

Yanfei Xu May 30, 2023, 9:25 a.m. UTC
  After 'commit 2a41ccee2fdc ("iommu/vt-d: Change
iommu_enable/disable_translation to return void")', init_iommu_hw() only
returns 0. If statement for return value of this function is meaningless.
Hence change init_iommu_hw() to return viod and remove the dead code of
if statement in init_iommu_hw()

Signed-off-by: Yanfei Xu <yanfei.xu@intel.com>
---
 drivers/iommu/intel/iommu.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
  

Comments

Baolu Lu May 31, 2023, 3:24 a.m. UTC | #1
On 5/30/23 5:25 PM, Yanfei Xu wrote:
> After 'commit 2a41ccee2fdc ("iommu/vt-d: Change
> iommu_enable/disable_translation to return void")', init_iommu_hw() only
> returns 0. If statement for return value of this function is meaningless.
> Hence change init_iommu_hw() to return viod and remove the dead code of
> if statement in init_iommu_hw()
> 
> Signed-off-by: Yanfei Xu<yanfei.xu@intel.com>
> ---
>   drivers/iommu/intel/iommu.c | 12 ++----------
>   1 file changed, 2 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> index 8096273b034c..e98f1b122b49 100644
> --- a/drivers/iommu/intel/iommu.c
> +++ b/drivers/iommu/intel/iommu.c
> @@ -2963,7 +2963,7 @@ static void __init init_no_remapping_devices(void)
>   }
>   
>   #ifdef CONFIG_SUSPEND
> -static int init_iommu_hw(void)
> +static void init_iommu_hw(void)
>   {
>   	struct dmar_drhd_unit *drhd;
>   	struct intel_iommu *iommu = NULL;
> @@ -2988,8 +2988,6 @@ static int init_iommu_hw(void)
>   		iommu_enable_translation(iommu);
>   		iommu_disable_protect_mem_regions(iommu);
>   	}
> -
> -	return 0;

2966 static int init_iommu_hw(void)
2967 {
2968         struct dmar_drhd_unit *drhd;
2969         struct intel_iommu *iommu = NULL;
2970
2971         for_each_active_iommu(iommu, drhd)
2972                 if (iommu->qi)
2973                         dmar_reenable_qi(iommu);

dmar_reenable_qi() still possibly returns an error number. It's better
to pass this error number to the caller of init_iommu_hw()?

2974
2975         for_each_iommu(iommu, drhd) {
2976                 if (drhd->ignored) {

Best regards,
baolu
  
Yanfei Xu May 31, 2023, 6:55 a.m. UTC | #2
Hi Baolu,

On 5/31/2023 11:24 AM, Baolu Lu wrote:
> On 5/30/23 5:25 PM, Yanfei Xu wrote:
>> After 'commit 2a41ccee2fdc ("iommu/vt-d: Change
>> iommu_enable/disable_translation to return void")', init_iommu_hw() only
>> returns 0. If statement for return value of this function is 
>> meaningless.
>> Hence change init_iommu_hw() to return viod and remove the dead code of
>> if statement in init_iommu_hw()
>>
>> Signed-off-by: Yanfei Xu<yanfei.xu@intel.com>
>> ---
>>   drivers/iommu/intel/iommu.c | 12 ++----------
>>   1 file changed, 2 insertions(+), 10 deletions(-)
>>
>> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
>> index 8096273b034c..e98f1b122b49 100644
>> --- a/drivers/iommu/intel/iommu.c
>> +++ b/drivers/iommu/intel/iommu.c
>> @@ -2963,7 +2963,7 @@ static void __init init_no_remapping_devices(void)
>>   }
>>     #ifdef CONFIG_SUSPEND
>> -static int init_iommu_hw(void)
>> +static void init_iommu_hw(void)
>>   {
>>       struct dmar_drhd_unit *drhd;
>>       struct intel_iommu *iommu = NULL;
>> @@ -2988,8 +2988,6 @@ static int init_iommu_hw(void)
>>           iommu_enable_translation(iommu);
>>           iommu_disable_protect_mem_regions(iommu);
>>       }
>> -
>> -    return 0;
>
> 2966 static int init_iommu_hw(void)
> 2967 {
> 2968         struct dmar_drhd_unit *drhd;
> 2969         struct intel_iommu *iommu = NULL;
> 2970
> 2971         for_each_active_iommu(iommu, drhd)
> 2972                 if (iommu->qi)
> 2973                         dmar_reenable_qi(iommu);
>
> dmar_reenable_qi() still possibly returns an error number. It's better
> to pass this error number to the caller of init_iommu_hw()?
>
Event dmar_reenable_qi can return error number, but there is no caller
check it. As below, only these two places invoke it:
1. init_iommu_hw->dmar_reenable_qi
2. reenable_irq_remapping->dmar_reenable_qi

I think we can also convert dmar_reenable_qi() to return void:
diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c
index a3414afe11b0..1432483c79e8 100644
--- a/drivers/iommu/intel/dmar.c
+++ b/drivers/iommu/intel/dmar.c
@@ -2112,13 +2112,10 @@ int __init enable_drhd_fault_handling(void)
  /*
   * Re-enable Queued Invalidation interface.
   */
-int dmar_reenable_qi(struct intel_iommu *iommu)
+void dmar_reenable_qi(struct intel_iommu *iommu)
  {
-       if (!ecap_qis(iommu->ecap))
-               return -ENOENT;
-
-       if (!iommu->qi)
-               return -ENOENT;
+       WARN_ON(!iommu->qi || !ecap_qis(iommu->ecap))
+               return;

         /*
          * First disable queued invalidation.
@@ -2130,8 +2127,6 @@ int dmar_reenable_qi(struct intel_iommu *iommu)
          * invalidation.
          */
         __dmar_enable_qi(iommu);
-
-       return 0;
  }

 From my understanding, dmar_reenable_qi() is used in suspend/resume case,
so the extended cap of an existing IOMMU hardware is unlikely changed. As
for the check of iommu->qi, if dmar_reenable_qi() can be invoked all is
depended on the no-NULL of iommu->qi at first. How about using WARN_ON for
both of them to simply this function.

Thanks,
Yanfei

> 2974
> 2975         for_each_iommu(iommu, drhd) {
> 2976                 if (drhd->ignored) {
>
> Best regards,
> baolu
  
Baolu Lu June 1, 2023, 1:15 a.m. UTC | #3
On 5/31/23 2:55 PM, Yanfei Xu wrote:
> 
> On 5/31/2023 11:24 AM, Baolu Lu wrote:
>> On 5/30/23 5:25 PM, Yanfei Xu wrote:
>>> After 'commit 2a41ccee2fdc ("iommu/vt-d: Change
>>> iommu_enable/disable_translation to return void")', init_iommu_hw() only
>>> returns 0. If statement for return value of this function is 
>>> meaningless.
>>> Hence change init_iommu_hw() to return viod and remove the dead code of
>>> if statement in init_iommu_hw()
>>>
>>> Signed-off-by: Yanfei Xu<yanfei.xu@intel.com>
>>> ---
>>>   drivers/iommu/intel/iommu.c | 12 ++----------
>>>   1 file changed, 2 insertions(+), 10 deletions(-)
>>>
>>> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
>>> index 8096273b034c..e98f1b122b49 100644
>>> --- a/drivers/iommu/intel/iommu.c
>>> +++ b/drivers/iommu/intel/iommu.c
>>> @@ -2963,7 +2963,7 @@ static void __init init_no_remapping_devices(void)
>>>   }
>>>     #ifdef CONFIG_SUSPEND
>>> -static int init_iommu_hw(void)
>>> +static void init_iommu_hw(void)
>>>   {
>>>       struct dmar_drhd_unit *drhd;
>>>       struct intel_iommu *iommu = NULL;
>>> @@ -2988,8 +2988,6 @@ static int init_iommu_hw(void)
>>>           iommu_enable_translation(iommu);
>>>           iommu_disable_protect_mem_regions(iommu);
>>>       }
>>> -
>>> -    return 0;
>>
>> 2966 static int init_iommu_hw(void)
>> 2967 {
>> 2968         struct dmar_drhd_unit *drhd;
>> 2969         struct intel_iommu *iommu = NULL;
>> 2970
>> 2971         for_each_active_iommu(iommu, drhd)
>> 2972                 if (iommu->qi)
>> 2973                         dmar_reenable_qi(iommu);
>>
>> dmar_reenable_qi() still possibly returns an error number. It's better
>> to pass this error number to the caller of init_iommu_hw()?
>>
> Event dmar_reenable_qi can return error number, but there is no caller
> check it. As below, only these two places invoke it:
> 1. init_iommu_hw->dmar_reenable_qi
> 2. reenable_irq_remapping->dmar_reenable_qi
> 
> I think we can also convert dmar_reenable_qi() to return void:
> diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c
> index a3414afe11b0..1432483c79e8 100644
> --- a/drivers/iommu/intel/dmar.c
> +++ b/drivers/iommu/intel/dmar.c
> @@ -2112,13 +2112,10 @@ int __init enable_drhd_fault_handling(void)
>   /*
>    * Re-enable Queued Invalidation interface.
>    */
> -int dmar_reenable_qi(struct intel_iommu *iommu)
> +void dmar_reenable_qi(struct intel_iommu *iommu)
>   {
> -       if (!ecap_qis(iommu->ecap))
> -               return -ENOENT;
> -
> -       if (!iommu->qi)
> -               return -ENOENT;
> +       WARN_ON(!iommu->qi || !ecap_qis(iommu->ecap))
> +               return;
> 
>          /*
>           * First disable queued invalidation.
> @@ -2130,8 +2127,6 @@ int dmar_reenable_qi(struct intel_iommu *iommu)
>           * invalidation.
>           */
>          __dmar_enable_qi(iommu);
> -
> -       return 0;
>   }
> 
>  From my understanding, dmar_reenable_qi() is used in suspend/resume case,
> so the extended cap of an existing IOMMU hardware is unlikely changed. As
> for the check of iommu->qi, if dmar_reenable_qi() can be invoked all is
> depended on the no-NULL of iommu->qi at first. How about using WARN_ON for
> both of them to simply this function.

This seems to be heading in the opposite direction. Actually any
operation may succeed or fail.

diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index b871a6afd803..ecc2007a96f9 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -2967,10 +2967,13 @@ static int init_iommu_hw(void)
  {
         struct dmar_drhd_unit *drhd;
         struct intel_iommu *iommu = NULL;
+       int ret;

-       for_each_active_iommu(iommu, drhd)
-               if (iommu->qi)
-                       dmar_reenable_qi(iommu);
+       for_each_active_iommu(iommu, drhd) {
+               ret = dmar_reenable_qi(iommu);
+               if (ret)
+                       return ret;
+       }

         for_each_iommu(iommu, drhd) {
                 if (drhd->ignored) {

Best regards,
baolu
  
Yanfei Xu June 1, 2023, 7:49 a.m. UTC | #4
On 6/1/2023 9:15 AM, Baolu Lu wrote:
> On 5/31/23 2:55 PM, Yanfei Xu wrote:
>>
>> On 5/31/2023 11:24 AM, Baolu Lu wrote:
>>> On 5/30/23 5:25 PM, Yanfei Xu wrote:
>>>> After 'commit 2a41ccee2fdc ("iommu/vt-d: Change
>>>> iommu_enable/disable_translation to return void")', init_iommu_hw() 
>>>> only
>>>> returns 0. If statement for return value of this function is 
>>>> meaningless.
>>>> Hence change init_iommu_hw() to return viod and remove the dead 
>>>> code of
>>>> if statement in init_iommu_hw()
>>>>
>>>> Signed-off-by: Yanfei Xu<yanfei.xu@intel.com>
>>>> ---
>>>>   drivers/iommu/intel/iommu.c | 12 ++----------
>>>>   1 file changed, 2 insertions(+), 10 deletions(-)
>>>>
>>>> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
>>>> index 8096273b034c..e98f1b122b49 100644
>>>> --- a/drivers/iommu/intel/iommu.c
>>>> +++ b/drivers/iommu/intel/iommu.c
>>>> @@ -2963,7 +2963,7 @@ static void __init 
>>>> init_no_remapping_devices(void)
>>>>   }
>>>>     #ifdef CONFIG_SUSPEND
>>>> -static int init_iommu_hw(void)
>>>> +static void init_iommu_hw(void)
>>>>   {
>>>>       struct dmar_drhd_unit *drhd;
>>>>       struct intel_iommu *iommu = NULL;
>>>> @@ -2988,8 +2988,6 @@ static int init_iommu_hw(void)
>>>>           iommu_enable_translation(iommu);
>>>>           iommu_disable_protect_mem_regions(iommu);
>>>>       }
>>>> -
>>>> -    return 0;
>>>
>>> 2966 static int init_iommu_hw(void)
>>> 2967 {
>>> 2968         struct dmar_drhd_unit *drhd;
>>> 2969         struct intel_iommu *iommu = NULL;
>>> 2970
>>> 2971         for_each_active_iommu(iommu, drhd)
>>> 2972                 if (iommu->qi)
>>> 2973                         dmar_reenable_qi(iommu);
>>>
>>> dmar_reenable_qi() still possibly returns an error number. It's better
>>> to pass this error number to the caller of init_iommu_hw()?
>>>
>> Event dmar_reenable_qi can return error number, but there is no caller
>> check it. As below, only these two places invoke it:
>> 1. init_iommu_hw->dmar_reenable_qi
>> 2. reenable_irq_remapping->dmar_reenable_qi
>>
>> I think we can also convert dmar_reenable_qi() to return void:
>> diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c
>> index a3414afe11b0..1432483c79e8 100644
>> --- a/drivers/iommu/intel/dmar.c
>> +++ b/drivers/iommu/intel/dmar.c
>> @@ -2112,13 +2112,10 @@ int __init enable_drhd_fault_handling(void)
>>   /*
>>    * Re-enable Queued Invalidation interface.
>>    */
>> -int dmar_reenable_qi(struct intel_iommu *iommu)
>> +void dmar_reenable_qi(struct intel_iommu *iommu)
>>   {
>> -       if (!ecap_qis(iommu->ecap))
>> -               return -ENOENT;
>> -
>> -       if (!iommu->qi)
>> -               return -ENOENT;
>> +       WARN_ON(!iommu->qi || !ecap_qis(iommu->ecap))
>> +               return;
>>
>>          /*
>>           * First disable queued invalidation.
>> @@ -2130,8 +2127,6 @@ int dmar_reenable_qi(struct intel_iommu *iommu)
>>           * invalidation.
>>           */
>>          __dmar_enable_qi(iommu);
>> -
>> -       return 0;
>>   }
>>
>>  From my understanding, dmar_reenable_qi() is used in suspend/resume 
>> case,
>> so the extended cap of an existing IOMMU hardware is unlikely 
>> changed. As
>> for the check of iommu->qi, if dmar_reenable_qi() can be invoked all is
>> depended on the no-NULL of iommu->qi at first. How about using 
>> WARN_ON for
>> both of them to simply this function.
>
> This seems to be heading in the opposite direction. Actually any
> operation may succeed or fail.
>
> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> index b871a6afd803..ecc2007a96f9 100644
> --- a/drivers/iommu/intel/iommu.c
> +++ b/drivers/iommu/intel/iommu.c
> @@ -2967,10 +2967,13 @@ static int init_iommu_hw(void)
>  {
>         struct dmar_drhd_unit *drhd;
>         struct intel_iommu *iommu = NULL;
> +       int ret;
>
> -       for_each_active_iommu(iommu, drhd)
> -               if (iommu->qi)
> -                       dmar_reenable_qi(iommu);
> +       for_each_active_iommu(iommu, drhd) {
> +               ret = dmar_reenable_qi(iommu);
> +               if (ret)
> +                       return ret;

For confirm, you mean this?
@@ -2967,10 +2970,14 @@ static int init_iommu_hw(void)
  {
         struct dmar_drhd_unit *drhd;
         struct intel_iommu *iommu = NULL;
+       int ret;

-       for_each_active_iommu(iommu, drhd)
+       for_each_active_iommu(iommu, drhd) {
                 if (iommu->qi)
-                       dmar_reenable_qi(iommu);
+                       ret = dmar_reenable_qi(iommu);
+               if (ret)
+                       return ret;
+       }

If drop NULL value check of iommu->qi, dmar_reenable_qi() may
return minus value to break the loop of dmar_reenable_qi as
one of iommus doesn't set up iommu->qi.

Thanks,
Yanfei

> + }
>
>         for_each_iommu(iommu, drhd) {
>                 if (drhd->ignored) {
>
> Best regards,
> baolu
  

Patch

diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 8096273b034c..e98f1b122b49 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -2963,7 +2963,7 @@  static void __init init_no_remapping_devices(void)
 }
 
 #ifdef CONFIG_SUSPEND
-static int init_iommu_hw(void)
+static void init_iommu_hw(void)
 {
 	struct dmar_drhd_unit *drhd;
 	struct intel_iommu *iommu = NULL;
@@ -2988,8 +2988,6 @@  static int init_iommu_hw(void)
 		iommu_enable_translation(iommu);
 		iommu_disable_protect_mem_regions(iommu);
 	}
-
-	return 0;
 }
 
 static void iommu_flush_all(void)
@@ -3051,13 +3049,7 @@  static void iommu_resume(void)
 	struct intel_iommu *iommu = NULL;
 	unsigned long flag;
 
-	if (init_iommu_hw()) {
-		if (force_on)
-			panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
-		else
-			WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
-		return;
-	}
+	init_iommu_hw();
 
 	for_each_active_iommu(iommu, drhd) {