Skip to contentSkip to conversation

iommu/vt-d: fix system hang on reboot -f

PATCH Superseded Yunhui Cui 3 participants 8 messages Feb 20, 2025 – Feb 24, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yunhui Cui

author
#0 lore raw

PATCH 1/1 iommu/vt-d: fix system hang on reboot -f

When entering intel_iommu_shutdown, system interrupts are disabled, and the reboot process might be scheduled out by down_write(). If the scheduled process does not yield (e.g., while(1)), the system will hang.

---
 drivers/iommu/intel/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +2 -1
index cc46098f875b..76a1d83b46bf 100644
@@ -2871,7 +2871,8 @@ void intel_iommu_shutdown(void)
 	if (no_iommu || dmar_disabled)
 		return;
 
-	down_write(&dmar_global_lock);
+	if (!down_write_trylock(&dmar_global_lock))
+		return;
 
 	/* Disable PMRs explicitly here. */
 	for_each_iommu(iommu, drhd)
Signature · 1 line
2.39.2
#1 lore raw
Quoted 1 line · Yunhui Cui

When entering intel_iommu_shutdown, system interrupts are disabled,

System interrupts were disabled ? you mean all interrupts were disabled when entering intel_iommu_shutdown(), perhaps it is not true, at least for upstream latest code.

Quoted 2 lines

and the reboot process might be scheduled out by down_write(). If the scheduled process does not yield (e.g., while(1)), the system will hang.

No NMI lockup watchdog jumping out here ?

Thanks,
Ethan

Quoted 20 lines
---
  drivers/iommu/intel/iommu.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +2 -1
index cc46098f875b..76a1d83b46bf 100644
@@ -2871,7 +2871,8 @@ void intel_iommu_shutdown(void)
  	if (no_iommu || dmar_disabled)
  		return;
 
-	down_write(&dmar_global_lock);
+	if (!down_write_trylock(&dmar_global_lock))
+		return;
 
  	/* Disable PMRs explicitly here. */
  	for_each_iommu(iommu, drhd)
Signature · 1 line
"firm, enduring, strong, and long-lived"
author
#2 lore raw

Hi Ethan,

Quoted 5 lines · Ethan Zhao
Quoted 1 line · Yunhui Cui

When entering intel_iommu_shutdown, system interrupts are disabled,

Quoted 8 lines

System interrupts were disabled ? you mean all interrupts were disabled when entering intel_iommu_shutdown(), perhaps it is not true, at least for upstream latest code.

Quoted 2 lines

and the reboot process might be scheduled out by down_write(). If the scheduled process does not yield (e.g., while(1)), the system will hang.

No NMI lockup watchdog jumping out here ?

Steps to reproduce:

1. Avoid return in:
if (no_iommu || dmar_disabled)

    return;

2. Write a.out with while(1).

3. ./a.out &; reboot -f.

4. Observe. Send NMI via BIOS to check system response.

5. Add console=ttyS0,115200 to cmdline to increase reproduction chance.

Let's continue discussing based on the above.

Quoted 22 lines

Thanks,
Ethan

Quoted 18 lines
---
  drivers/iommu/intel/iommu.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +2 -1
index cc46098f875b..76a1d83b46bf 100644
@@ -2871,7 +2871,8 @@ void intel_iommu_shutdown(void)
      if (no_iommu || dmar_disabled)
              return;
 
-     down_write(&dmar_global_lock);
+     if (!down_write_trylock(&dmar_global_lock))
+             return;
 
Quoted 6 lines

drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2878

Quoted 2 lines
drivers/iommu/intel/iommu.c +0 -0
      /* Disable PMRs explicitly here. */
      for_each_iommu(iommu, drhd)

--
"firm, enduring, strong, and long-lived"

Thanks,
Yunhui

#3 lore raw
Quoted 21 lines · yunhui cui

Hi Ethan,

Quoted 10 lines · Ethan Zhao
Quoted 1 line · Yunhui Cui

When entering intel_iommu_shutdown, system interrupts are disabled,

System interrupts were disabled ? you mean all interrupts were disabled when entering intel_iommu_shutdown(), perhaps it is not true, at least for upstream latest code.

Quoted 2 lines

and the reboot process might be scheduled out by down_write(). If the scheduled process does not yield (e.g., while(1)), the system will hang.

No NMI lockup watchdog jumping out here ?

Steps to reproduce:

1. Avoid return in:
if (no_iommu || dmar_disabled)

     return;

2. Write a.out with while(1).

Quoted 7 lines

3. ./a.out &; reboot -f.

4. Observe. Send NMI via BIOS to check system response.

5. Add console=ttyS0,115200 to cmdline to increase reproduction chance.

Let's continue discussing based on the above.

I will try these steps to reproduce.

Per the lastest upstream code, the local processor's interrupt mask is cleaned. so

the processor could accept interrupts and handle them. and lagacy interrupt should

be restored for later boot if there is lagacy device and as to NMI, no one could stop

it. In a short, perhaps it is fact under your hardware configureation that no interrupt

event come in to kick the scheduler to run when the a.out (while(1)) got scheduled in,

but not because all system interrupts are disabled.

Thanks,
Ethan

Quoted 28 lines
Quoted 16 lines

Thanks,
Ethan

Quoted 13 lines
---
   drivers/iommu/intel/iommu.c | 3 ++-
   1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index cc46098f875b..76a1d83b46bf 100644
@@ -2871,7 +2871,8 @@ void intel_iommu_shutdown(void)
       if (no_iommu || dmar_disabled)
               return;
 
Quoted 9 lines

drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2878

Quoted 6 lines
drivers/iommu/intel/iommu.c +2 -1
-     down_write(&dmar_global_lock);
+     if (!down_write_trylock(&dmar_global_lock))
+             return;
 
       /* Disable PMRs explicitly here. */
       for_each_iommu(iommu, drhd)

--
"firm, enduring, strong, and long-lived"

Thanks,
Yunhui

Signature · 1 line
"firm, enduring, strong, and long-lived"
#4 lore raw
In reply to yunhui cui
Quoted 17 lines · yunhui cui

Hi Ethan,

Quoted 10 lines · Ethan Zhao
Quoted 1 line · Yunhui Cui

When entering intel_iommu_shutdown, system interrupts are disabled,

System interrupts were disabled ? you mean all interrupts were disabled when entering intel_iommu_shutdown(), perhaps it is not true, at least for upstream latest code.

Quoted 2 lines

and the reboot process might be scheduled out by down_write(). If the scheduled process does not yield (e.g., while(1)), the system will hang.

No NMI lockup watchdog jumping out here ?

Steps to reproduce:

1. Avoid return in:
if (no_iommu || dmar_disabled)

Quoted 7 lines
     return;

2. Write a.out with while(1).

3. ./a.out &; reboot -f.

4. Observe. Send NMI via BIOS to check system response.

Via BMC ? There is 'NMI' hardware physical button on some machines to trigger

NMI to OS for diagnostic purpose, you could check your box for that. but no luck,

there is no NMI trigger in my GNR BMC.

Thanks,
Ethan

Quoted 32 lines

5. Add console=ttyS0,115200 to cmdline to increase reproduction chance.

Let's continue discussing based on the above.

Quoted 16 lines

Thanks,
Ethan

Quoted 13 lines
---
   drivers/iommu/intel/iommu.c | 3 ++-
   1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index cc46098f875b..76a1d83b46bf 100644
@@ -2871,7 +2871,8 @@ void intel_iommu_shutdown(void)
       if (no_iommu || dmar_disabled)
               return;
 
Quoted 9 lines

drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2878

Quoted 6 lines
drivers/iommu/intel/iommu.c +2 -1
-     down_write(&dmar_global_lock);
+     if (!down_write_trylock(&dmar_global_lock))
+             return;
 
       /* Disable PMRs explicitly here. */
       for_each_iommu(iommu, drhd)

--
"firm, enduring, strong, and long-lived"

Thanks,
Yunhui

Signature · 1 line
"firm, enduring, strong, and long-lived"
maintainer
#5 lore raw
In reply to Yunhui Cui
Quoted 14 lines · Yunhui Cui

When entering intel_iommu_shutdown, system interrupts are disabled, and the reboot process might be scheduled out by down_write(). If the scheduled process does not yield (e.g., while(1)), the system will hang.

---
  drivers/iommu/intel/iommu.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index cc46098f875b..76a1d83b46bf 100644

drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2875

Quoted 6 lines
drivers/iommu/intel/iommu.c +2 -1
@@ -2871,7 +2871,8 @@ void intel_iommu_shutdown(void)
  	if (no_iommu || dmar_disabled)
  		return;
 
-	down_write(&dmar_global_lock);
+	if (!down_write_trylock(&dmar_global_lock))
+		return;

If system interrupts are disabled here, locking is unnecessary. Hotplug operations depend on interrupt events, so it's better to remove the lock. The shutdown helper then appears like this:

void intel_iommu_shutdown(void)

{
        struct dmar_drhd_unit *drhd;
        struct intel_iommu *iommu = NULL;

        if (no_iommu || dmar_disabled)
                return;

        /*
         * System interrupts are disabled when it reaches here. Locking
         * is unnecessary when iterating the IOMMU list.
         */
        list_for_each_entry(drhd, &dmar_drhd_units, list) {
                if (drhd->ignored)
                        continue;

                iommu = drhd->iommu;
                /* Disable PMRs explicitly here. */
                iommu_disable_protect_mem_regions(iommu);
                iommu_disable_translation(iommu);
        }
}

Does it work for you?

Thanks,
baolu

author
#6 lore raw

Hi Baolu,

Quoted 19 lines · Baolu Lu
Quoted 17 lines · Yunhui Cui

When entering intel_iommu_shutdown, system interrupts are disabled, and the reboot process might be scheduled out by down_write(). If the scheduled process does not yield (e.g., while(1)), the system will hang.

---
  drivers/iommu/intel/iommu.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index cc46098f875b..76a1d83b46bf 100644
@@ -2871,7 +2871,8 @@ void intel_iommu_shutdown(void)
      if (no_iommu || dmar_disabled)
              return;
 
Quoted 7 lines

drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2875

Quoted 3 lines
drivers/iommu/intel/iommu.c +2 -1
-     down_write(&dmar_global_lock);
+     if (!down_write_trylock(&dmar_global_lock))
+             return;

If system interrupts are disabled here, locking is unnecessary. Hotplug operations depend on interrupt events, so it's better to remove the lock. The shutdown helper then appears like this:

Currently, intel_iommu_shutdown() is only called by native_machine_shutdown(). The down_write/up operations can be removed. Even if there's a hardware access error, IOMMU_WAIT_OP() will trigger a panic().

Quoted 24 lines

void intel_iommu_shutdown(void)

{
         struct dmar_drhd_unit *drhd;
         struct intel_iommu *iommu = NULL;

         if (no_iommu || dmar_disabled)
                 return;

         /*
          * System interrupts are disabled when it reaches here. Locking
          * is unnecessary when iterating the IOMMU list.
          */
         list_for_each_entry(drhd, &dmar_drhd_units, list) {
                 if (drhd->ignored)
                         continue;

                 iommu = drhd->iommu;
                 /* Disable PMRs explicitly here. */
                 iommu_disable_protect_mem_regions(iommu);
                 iommu_disable_translation(iommu);
         }
}
Quoted 1 line

Does it work for you?

Yes.

Quoted 3 lines

Thanks,
baolu

Thanks,
Yunhui

#7 lore raw
In reply to Baolu Lu
Quoted 10 lines · Baolu Lu
Quoted 9 lines · Yunhui Cui

When entering intel_iommu_shutdown, system interrupts are disabled, and the reboot process might be scheduled out by down_write(). If the scheduled process does not yield (e.g., while(1)), the system will hang.

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

  1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 8 lines

drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2873

Quoted 8 lines
drivers/iommu/intel/iommu.c +0 -0
index cc46098f875b..76a1d83b46bf 100644
@@ -2871,7 +2871,8 @@ void intel_iommu_shutdown(void)
       if (no_iommu || dmar_disabled)
           return;
   -    down_write(&dmar_global_lock);

Only BSP is running at this point, no DMAR concurrency access protection is needed anyore, even there is interrupt (only legacy & NMI) coming in, it is impossible to for scheduler to run any other iommu access code.**
Thanks,
Ethan**

Quoted 35 lines

drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2875

Quoted 2 lines
drivers/iommu/intel/iommu.c +2 -0
+    if (!down_write_trylock(&dmar_global_lock))
+        return;

If system interrupts are disabled here, locking is unnecessary. Hotplug operations depend on interrupt events, so it's better to remove the lock. The shutdown helper then appears like this:

void intel_iommu_shutdown(void)
{
        struct dmar_drhd_unit *drhd;
        struct intel_iommu *iommu = NULL;

        if (no_iommu || dmar_disabled)
                return;

        /*
         * System interrupts are disabled when it reaches here. Locking
         * is unnecessary when iterating the IOMMU list.
         */
        list_for_each_entry(drhd, &dmar_drhd_units, list) { if (drhd->ignored)
                        continue;

                iommu = drhd->iommu;
                /* Disable PMRs explicitly here. */ iommu_disable_protect_mem_regions(iommu); iommu_disable_translation(iommu); }
}

Does it work for you?

Thanks,
baolu

Signature · 1 line
"firm, enduring, strong, and long-lived"