Skip to contentSkip to conversation

x86/bus_lock: Don't assume the init value of DEBUGCTLMSR.BUS_LOCK_DETECT to be zero

PATCH Superseded Chenyi Qiang 2 participants 5 messages Sep 1, 2021 – Oct 25, 2021 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/bus_lock: Don't assume the init value of DEBUGCTLMSR.BUS_LOCK_DETECT to be zero

It's possible that BIOS/firmware has set DEBUGCTLMSR_BUS_LOCK_DETECT, or this kernel has been kexec'd from a kernel that enabled bus lock detection.

Disable bus lock detection explicitly if not wanted.

---
 arch/x86/kernel/cpu/intel.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/intel.c +14 -13
index 8321c43554a1..38dda04d9342 100644
@@ -1152,22 +1152,23 @@ static void bus_lock_init(void)
 {
 	u64 val;
 
-	/*
-	 * Warn and fatal are handled by #AC for split lock if #AC for
-	 * split lock is supported.
-	 */
-	if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) ||
-	    (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
-	    (sld_state == sld_warn || sld_state == sld_fatal)) ||
-	    sld_state == sld_off)
+	if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
 		return;
 
-	/*
-	 * Enable #DB for bus lock. All bus locks are handled in #DB except
-	 * split locks are handled in #AC in the fatal case.
-	 */
 	rdmsrl(MSR_IA32_DEBUGCTLMSR, val);
-	val |= DEBUGCTLMSR_BUS_LOCK_DETECT;
+
+	if ((boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
+	    (sld_state == sld_warn || sld_state == sld_fatal)) ||
+	    sld_state == sld_off) {
+		/*
+		 * Warn and fatal are handled by #AC for split lock if #AC for
+		 * split lock is supported.
+		 */
+		val &= ~DEBUGCTLMSR_BUS_LOCK_DETECT;
+	} else {
+		val |= DEBUGCTLMSR_BUS_LOCK_DETECT;
+	}
+
 	wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
 }
 
Signature · 1 line
2.17.1
#1 lore raw

Kindly ping for this minor change.

Thanks
Chenyi

Quoted 54 lines · Chenyi Qiang

It's possible that BIOS/firmware has set DEBUGCTLMSR_BUS_LOCK_DETECT, or this kernel has been kexec'd from a kernel that enabled bus lock detection.

Disable bus lock detection explicitly if not wanted.

---
  arch/x86/kernel/cpu/intel.c | 27 ++++++++++++++-------------
  1 file changed, 14 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/intel.c +14 -13
index 8321c43554a1..38dda04d9342 100644
@@ -1152,22 +1152,23 @@ static void bus_lock_init(void)
  {
  	u64 val;
 
-	/*
-	 * Warn and fatal are handled by #AC for split lock if #AC for
-	 * split lock is supported.
-	 */
-	if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) ||
-	    (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
-	    (sld_state == sld_warn || sld_state == sld_fatal)) ||
-	    sld_state == sld_off)
+	if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
  		return;
 
-	/*
-	 * Enable #DB for bus lock. All bus locks are handled in #DB except
-	 * split locks are handled in #AC in the fatal case.
-	 */
  	rdmsrl(MSR_IA32_DEBUGCTLMSR, val);
-	val |= DEBUGCTLMSR_BUS_LOCK_DETECT;
+
+	if ((boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
+	    (sld_state == sld_warn || sld_state == sld_fatal)) ||
+	    sld_state == sld_off) {
+		/*
+		 * Warn and fatal are handled by #AC for split lock if #AC for
+		 * split lock is supported.
+		 */
+		val &= ~DEBUGCTLMSR_BUS_LOCK_DETECT;
+	} else {
+		val |= DEBUGCTLMSR_BUS_LOCK_DETECT;
+	}
+
  	wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
  }
 
#2 lore raw

Reminder for this minor fix.

Quoted 57 lines · Chenyi Qiang

Kindly ping for this minor change.

Thanks
Chenyi

Quoted 51 lines · Chenyi Qiang

It's possible that BIOS/firmware has set DEBUGCTLMSR_BUS_LOCK_DETECT, or this kernel has been kexec'd from a kernel that enabled bus lock detection.

Disable bus lock detection explicitly if not wanted.

---
  arch/x86/kernel/cpu/intel.c | 27 ++++++++++++++-------------

  1 file changed, 14 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/intel.c +14 -13
index 8321c43554a1..38dda04d9342 100644
@@ -1152,22 +1152,23 @@ static void bus_lock_init(void)
   {
       u64 val;
-    /*
-     * Warn and fatal are handled by #AC for split lock if #AC for
-     * split lock is supported.
-     */
-    if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) ||
-        (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
-        (sld_state == sld_warn || sld_state == sld_fatal)) ||
-        sld_state == sld_off)
+    if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
           return;
-    /*
-     * Enable #DB for bus lock. All bus locks are handled in #DB except
-     * split locks are handled in #AC in the fatal case.
-     */
       rdmsrl(MSR_IA32_DEBUGCTLMSR, val);
-    val |= DEBUGCTLMSR_BUS_LOCK_DETECT;
+
+    if ((boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
+        (sld_state == sld_warn || sld_state == sld_fatal)) ||
+        sld_state == sld_off) {
+        /*
+         * Warn and fatal are handled by #AC for split lock if #AC for
+         * split lock is supported.
+         */
+        val &= ~DEBUGCTLMSR_BUS_LOCK_DETECT;
+    } else {
+        val |= DEBUGCTLMSR_BUS_LOCK_DETECT;
+    }
+
       wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
   }
 
#3 lore raw
In reply to Chenyi Qiang
Quoted 3 lines · Chenyi Qiang

It's possible that BIOS/firmware has set DEBUGCTLMSR_BUS_LOCK_DETECT, or this kernel has been kexec'd from a kernel that enabled bus lock detection.

This feels like the kernel should explicitly zero out the entire MSR somewhere in the generic boot flow. E.g. something like this somewhere.

#ifndef CONFIG_X86_DEBUGCTLMSR
	if (boot_cpu_data.x86 < 6)
		return;

#endifa

	wrmsrl(MSR_IA32_DEBUGCTLMSR, 0);
#4 lore raw
Quoted 8 lines · Sean Christopherson
Quoted 3 lines · Chenyi Qiang

It's possible that BIOS/firmware has set DEBUGCTLMSR_BUS_LOCK_DETECT, or this kernel has been kexec'd from a kernel that enabled bus lock detection.

This feels like the kernel should explicitly zero out the entire MSR somewhere in the generic boot flow. E.g. something like this somewhere.

Yes. Meanwhile, I think kernel code prefers to explicitly set/clear the control bit according to the parameter. Maybe both changes should be applied.

Quoted 7 lines
#ifndef CONFIG_X86_DEBUGCTLMSR
	if (boot_cpu_data.x86 < 6)
		return;

#endifa

	wrmsrl(MSR_IA32_DEBUGCTLMSR, 0);