Skip to contentSkip to conversation

Misc cleanup for iommu/vt-d

PATCH Superseded Yanfei Xu 3 participants 12 messages May 30, 2023 – Jun 2, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yanfei Xu

author
#0 lore raw

cover Misc cleanup for iommu/vt-d

No functional changes expected.

Yanfei Xu (2):
iommu/vt-d: Remove the dead code in init_iommu_hw() iommu/vt-d: Use BUG_ON to check NULL value of 'table'

 drivers/iommu/intel/iommu.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

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

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()

---
 drivers/iommu/intel/iommu.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/iommu/intel/iommu.c +2 -10
index 8096273b034c..e98f1b122b49 100644
@@ -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) {
 
Signature · 1 line
2.34.1
maintainer
#2 lore raw

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

Quoted 25 lines · Yanfei Xu

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()

---
  drivers/iommu/intel/iommu.c | 12 ++----------
  1 file changed, 2 insertions(+), 10 deletions(-)
drivers/iommu/intel/iommu.c +1 -1
index 8096273b034c..e98f1b122b49 100644
@@ -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;

drivers/iommu/intel/iommu.c static int init_iommu_hw(void) · line 2992

Quoted 5 lines
drivers/iommu/intel/iommu.c +0 -2
@@ -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

author
#3 lore raw

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

Hi Baolu,

Quoted 40 lines · Baolu Lu
Quoted 24 lines · Yanfei Xu

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()

---
  drivers/iommu/intel/iommu.c | 12 ++----------

  1 file changed, 2 insertions(+), 10 deletions(-)
drivers/iommu/intel/iommu.c +1 -1
index 8096273b034c..e98f1b122b49 100644
@@ -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;

drivers/iommu/intel/iommu.c static int init_iommu_hw(void) · line 2992

Quoted 5 lines
drivers/iommu/intel/iommu.c +0 -2
@@ -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);

Quoted 3 lines

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:

drivers/iommu/intel/dmar.c +0 -0
index a3414afe11b0..1432483c79e8 100644
@@ -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

Quoted 6 lines

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

Best regards,
baolu

maintainer
#4 lore raw

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

Quoted 81 lines · Yanfei Xu
Quoted 40 lines · Baolu Lu
Quoted 24 lines · Yanfei Xu

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()

---
  drivers/iommu/intel/iommu.c | 12 ++----------

  1 file changed, 2 insertions(+), 10 deletions(-)
drivers/iommu/intel/iommu.c +1 -1
index 8096273b034c..e98f1b122b49 100644
@@ -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;

drivers/iommu/intel/iommu.c static int init_iommu_hw(void) · line 2992

Quoted 5 lines
drivers/iommu/intel/iommu.c +0 -2
@@ -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);

Quoted 3 lines

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:

drivers/iommu/intel/dmar.c +3 -8
index a3414afe11b0..1432483c79e8 100644
@@ -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,
Quoted 4 lines

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.

drivers/iommu/intel/iommu.c +6 -3
index b871a6afd803..ecc2007a96f9 100644
@@ -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

author
#5 lore raw

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

Quoted 98 lines · Baolu Lu
Quoted 82 lines · Yanfei Xu
Quoted 41 lines · Baolu Lu
Quoted 25 lines · Yanfei Xu

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()

---
  drivers/iommu/intel/iommu.c | 12 ++----------

  1 file changed, 2 insertions(+), 10 deletions(-)
drivers/iommu/intel/iommu.c +1 -1
index 8096273b034c..e98f1b122b49 100644
@@ -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;

drivers/iommu/intel/iommu.c static int init_iommu_hw(void) · line 2992

Quoted 5 lines
drivers/iommu/intel/iommu.c +0 -2
@@ -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);

Quoted 3 lines

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:

drivers/iommu/intel/dmar.c +3 -8
index a3414afe11b0..1432483c79e8 100644
@@ -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,
Quoted 4 lines

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.

drivers/iommu/intel/iommu.c +0 -0
index b871a6afd803..ecc2007a96f9 100644
@@ -2967,10 +2967,13 @@ static int init_iommu_hw(void)
  {
         struct dmar_drhd_unit *drhd;

drivers/iommu/intel/iommu.c static int init_iommu_hw(void) · line 2975

Quoted 10 lines
drivers/iommu/intel/iommu.c +5 -3
         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

Quoted 7 lines
drivers/iommu/intel/iommu.c +1 -0
+ }
 
         for_each_iommu(iommu, drhd) {
                 if (drhd->ignored) {

Best regards,
baolu

author
#6 lore raw

PATCH 2/2 iommu/vt-d: Use BUG_ON to check NULL value of 'table'

In reply to Yanfei Xu

Checking NULL value of 'table' variable deserves a BUG_ON as the following code will trigger a crash by dereferencing the NULL 'table' pointer. Crash in advance with BUG_ON to avoid WARN_ON plus NULL pointer dereferencing can simplify the crash log.

---
 drivers/iommu/intel/iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +1 -1
index e98f1b122b49..8aa3bfdb7f95 100644
@@ -1944,7 +1944,7 @@ static int domain_context_mapping_one(struct dmar_domain *domain,
 	if (sm_supported(iommu)) {
 		unsigned long pds;
 
-		WARN_ON(!table);
+		BUG_ON(!table);
 
 		/* Setup the PASID DIR pointer: */
 		pds = context_get_sm_pds(table);
Signature · 1 line
2.34.1
maintainer
#7 lore raw

Re: [PATCH 2/2] iommu/vt-d: Use BUG_ON to check NULL value of 'table'

Quoted 15 lines · Yanfei Xu

Checking NULL value of 'table' variable deserves a BUG_ON as the following code will trigger a crash by dereferencing the NULL 'table' pointer. Crash in advance with BUG_ON to avoid WARN_ON plus NULL pointer dereferencing can simplify the crash log.

---
  drivers/iommu/intel/iommu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index e98f1b122b49..8aa3bfdb7f95 100644

drivers/iommu/intel/iommu.c static int domain_context_mapping_one(struct dmar_domain *domain, · line 1947

Quoted 5 lines
drivers/iommu/intel/iommu.c +1 -1
@@ -1944,7 +1944,7 @@ static int domain_context_mapping_one(struct dmar_domain *domain,
  	if (sm_supported(iommu)) {
  		unsigned long pds;
 
-		WARN_ON(!table);
+		BUG_ON(!table);

BUG_ON() is not recommended. Perhaps,

		if (!table)
			-ENODEV;

?

Best regards,
baolu

author
#8 lore raw

Re: [PATCH 2/2] iommu/vt-d: Use BUG_ON to check NULL value of 'table'

Hi Baolu,

Quoted 24 lines · Baolu Lu
Quoted 15 lines · Yanfei Xu

Checking NULL value of 'table' variable deserves a BUG_ON as the following code will trigger a crash by dereferencing the NULL 'table' pointer. Crash in advance with BUG_ON to avoid WARN_ON plus NULL pointer dereferencing can simplify the crash log.

---
  drivers/iommu/intel/iommu.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index e98f1b122b49..8aa3bfdb7f95 100644

drivers/iommu/intel/iommu.c static int domain_context_mapping_one(struct · line 1948

Quoted 5 lines
drivers/iommu/intel/iommu.c +1 -0
@@ -1944,7 +1944,7 @@ static int domain_context_mapping_one(struct 
 dmar_domain *domain,
       if (sm_supported(iommu)) {
           unsigned long pds;
   -        WARN_ON(!table);
+        BUG_ON(!table);

BUG_ON() is not recommended. Perhaps,

Quoted 3 lines

        if (!table)
            -ENODEV;

Agree:) It is always better to handle the error than crash kernel.

How about:

drivers/iommu/intel/iommu.c +0 -0
index 8096273b034c..7f077e3a4128 100644
@@ -1944,7 +1944,10 @@ static int domain_context_mapping_one(struct dmar_domain *domain,

        if (sm_supported(iommu)) {
                unsigned long pds;

-               WARN_ON(!table);
+               if (WARN_ON(!table)) {
+                       ret = -ENODEV;
+                       goto out_unlock;
+               }

Thanks,
Yanfei

Quoted 4 lines

?

Best regards,
baolu

maintainer
#9 lore raw

Re: [PATCH 2/2] iommu/vt-d: Use BUG_ON to check NULL value of 'table'

Quoted 36 lines · Yanfei Xu
Quoted 24 lines · Baolu Lu
Quoted 15 lines · Yanfei Xu

Checking NULL value of 'table' variable deserves a BUG_ON as the following code will trigger a crash by dereferencing the NULL 'table' pointer. Crash in advance with BUG_ON to avoid WARN_ON plus NULL pointer dereferencing can simplify the crash log.

---
  drivers/iommu/intel/iommu.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index e98f1b122b49..8aa3bfdb7f95 100644

drivers/iommu/intel/iommu.c static int domain_context_mapping_one(struct · line 1948

Quoted 5 lines
drivers/iommu/intel/iommu.c +1 -0
@@ -1944,7 +1944,7 @@ static int domain_context_mapping_one(struct 
 dmar_domain *domain,
       if (sm_supported(iommu)) {
           unsigned long pds;
   -        WARN_ON(!table);
+        BUG_ON(!table);

BUG_ON() is not recommended. Perhaps,

Quoted 3 lines

        if (!table)
            -ENODEV;

Agree:) It is always better to handle the error than crash kernel.

How about:

drivers/iommu/intel/iommu.c +0 -0
index 8096273b034c..7f077e3a4128 100644

drivers/iommu/intel/iommu.c static int domain_context_mapping_one(struct · line 1951

Quoted 9 lines
drivers/iommu/intel/iommu.c +4 -1
@@ -1944,7 +1944,10 @@ static int domain_context_mapping_one(struct 
 dmar_domain *domain,
         if (sm_supported(iommu)) {
                 unsigned long pds;
 
-               WARN_ON(!table);
+               if (WARN_ON(!table)) {
+                       ret = -ENODEV;
+                       goto out_unlock;
+               }

I'd recommend to remove this line directly. This pointer will be accessed in the following code, if empty "table" really happens, the kernel will report a NULL pointer reference warning at the first place.

In the same function, I also saw "WARN_ON(did == 0);". It's unnecessary as domain_id_iommu() will never return 0. Perhaps we can clean it up as well.

Best regards,
baolu

author
#10 lore raw

Re: [PATCH 2/2] iommu/vt-d: Use BUG_ON to check NULL value of 'table'

Quoted 51 lines · Baolu Lu
Quoted 36 lines · Yanfei Xu
Quoted 24 lines · Baolu Lu
Quoted 15 lines · Yanfei Xu

Checking NULL value of 'table' variable deserves a BUG_ON as the following code will trigger a crash by dereferencing the NULL 'table' pointer. Crash in advance with BUG_ON to avoid WARN_ON plus NULL pointer dereferencing can simplify the crash log.

---
  drivers/iommu/intel/iommu.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index e98f1b122b49..8aa3bfdb7f95 100644

drivers/iommu/intel/iommu.c static int domain_context_mapping_one(struct · line 1948

Quoted 5 lines
drivers/iommu/intel/iommu.c +1 -0
@@ -1944,7 +1944,7 @@ static int domain_context_mapping_one(struct 
 dmar_domain *domain,
       if (sm_supported(iommu)) {
           unsigned long pds;
   -        WARN_ON(!table);
+        BUG_ON(!table);

BUG_ON() is not recommended. Perhaps,

Quoted 3 lines

        if (!table)
            -ENODEV;

Agree:) It is always better to handle the error than crash kernel.

How about:

drivers/iommu/intel/iommu.c +0 -0
index 8096273b034c..7f077e3a4128 100644

drivers/iommu/intel/iommu.c static int domain_context_mapping_one(struct · line 1951

Quoted 9 lines
drivers/iommu/intel/iommu.c +4 -1
@@ -1944,7 +1944,10 @@ static int domain_context_mapping_one(struct 
 dmar_domain *domain,
          if (sm_supported(iommu)) {
                  unsigned long pds;
 
-               WARN_ON(!table);
+               if (WARN_ON(!table)) {
+                       ret = -ENODEV;
+                       goto out_unlock;
+               }

I'd recommend to remove this line directly. This pointer will be accessed in the following code, if empty "table" really happens, the kernel will report a NULL pointer reference warning at the first place.

Quoted 3 lines

In the same function, I also saw "WARN_ON(did == 0);". It's unnecessary as domain_id_iommu() will never return 0. Perhaps we can clean it up as well.

Will do in V2.

Thanks,
Yanfei

Quoted 3 lines

Best regards,
baolu

#11 lore raw

Re: [PATCH 2/2] iommu/vt-d: Use BUG_ON to check NULL value of 'table'

In reply to Baolu Lu
Quoted 23 lines · Baolu Lu
Quoted 15 lines · Yanfei Xu

Checking NULL value of 'table' variable deserves a BUG_ON as the following code will trigger a crash by dereferencing the NULL 'table' pointer. Crash in advance with BUG_ON to avoid WARN_ON plus NULL pointer dereferencing can simplify the crash log.

---
  drivers/iommu/intel/iommu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index e98f1b122b49..8aa3bfdb7f95 100644

drivers/iommu/intel/iommu.c static int domain_context_mapping_one(struct dmar_domain *domain, · line 1946

Quoted 4 lines
drivers/iommu/intel/iommu.c +1 -1
@@ -1944,7 +1944,7 @@ static int domain_context_mapping_one(struct dmar_domain *domain,
  	if (sm_supported(iommu)) {
  		unsigned long pds;
-		WARN_ON(!table);
+		BUG_ON(!table);

BUG_ON() is not recommended. Perhaps,

Quoted 4 lines
		if (!table)
			-ENODEV;

?

If it is not something you think needs active debugging then just let it crash on the NULL pointer deref. You get a nice backtrace from that already.

The additional value of the WARN is it gives you a line number, a prettier print and it might recover more cleanly (or not, it might just crash somewhere else).

Jason