Skip to contentSkip to conversation

x86 fixes for 3.11-rc2

GIT PULL George Spelvin 3 participants 5 messages Jul 20, 2013 – Jul 21, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

It's marginal with only two call sites, but would it be worth factoring out the write-back function? Something like this (untested) patch. It definitely makes the generated assembly cleaner.

(Signed-off-by: George Spelvin <[email protected]> if you want it.)

arch/x86/include/asm/msr.h +24 -0
index cb75028..8802d97 100644
@@ -171,6 +171,30 @@ static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
 	__err;							\
 })
 
+/*
+ * We have to check that we can write back the value, and not just
+ * read it.  At least on 90 nm Pentium M (Family 6, Model 13), reading
+ * an invalid MSR is not guaranteed to trap, see Erratum X4 in "Intel
+ * Pentium M Processor on 90 nm Process with 2-MB L2 Cache and Intel®
+ * Processor A100 and A110 on 90 nm process with 512-KB L2 Cache
+ * Specification Update".
+ */
+#define rdmsr_verysafe(msr, low, high)				\
+({								\
+	int __err;						\
+	asm volatile("2: rdmsr\n"				\
+		     "3: wrmsr ; xor %[err],%[err]\n"		\
+		     "1:\n\t"					\
+		     ".section .fixup,\"ax\"\n\t"		\
+		     "4:  mov %[fault],%[err] ; jmp 1b\n\t"	\
+		     ".previous\n\t"				\
+		     _ASM_EXTABLE(2b, 4b)			\
+		     _ASM_EXTABLE(3b, 4b)			\
+		     : [err] "=r" (__err), "=a" (*low), "=d" (*high) \
+		     : "c" (msr), [fault] "i" (-EIO));		\
+	__err;							\
+})
+
 static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
 {
 	int err;
arch/x86/kernel/acpi/sleep.c +6 -6
index b44577b..7c3f40c 100644
@@ -48,9 +48,9 @@ int acpi_suspend_lowlevel(void)
 #ifndef CONFIG_64BIT
 	native_store_gdt((struct desc_ptr *)&header->pmode_gdt);
 
-	if (!rdmsr_safe(MSR_EFER,
-			&header->pmode_efer_low,
-			&header->pmode_efer_high))
+	if (!rdmsr_verysafe(MSR_EFER,
+			    &header->pmode_efer_low,
+			    &header->pmode_efer_high))
 		header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_EFER);
 #endif /* !CONFIG_64BIT */
 
@@ -59,9 +59,9 @@ int acpi_suspend_lowlevel(void)
 		header->pmode_cr4 = read_cr4();
 		header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_CR4);
 	}
-	if (!rdmsr_safe(MSR_IA32_MISC_ENABLE,
-			&header->pmode_misc_en_low,
-			&header->pmode_misc_en_high))
+	if (!rdmsr_verysafe(MSR_IA32_MISC_ENABLE,
+			    &header->pmode_misc_en_low,
+			    &header->pmode_misc_en_high))
 		header->pmode_behavior |=
 			(1 << WAKEUP_BEHAVIOR_RESTORE_MISC_ENABLE);
 	header->realmode_flags = acpi_realmode_flags;
maintainer
#1 lore raw
Quoted 3 lines · George Spelvin

It's marginal with only two call sites, but would it be worth factoring out the write-back function? Something like this (untested) patch. It definitely makes the generated assembly cleaner.

I don't think that matters because this is called only once on suspend. Unless the cleaner assembly translates into a palpable speedup, which I doubt.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#2 lore raw
Quoted 3 lines · Borislav Petkov

I don't think that matters because this is called only once on suspend. Unless the cleaner assembly translates into a palpable speedup, which I doubt.

I was thinking about code *size*, actually; I agree that speed is too small to measure.

Clean code (21 bytes):

  4e:   b9 80 00 00 c0          mov    $0xc0000080,%ecx
  53:   0f 32                   rdmsr  
  55:   0f 30                   wrmsr  
  57:   31 f6                   xor    %esi,%esi
  59:   85 f6                   test   %esi,%esi
  5b:   89 43 14                mov    %eax,0x14(%ebx)
  5e:   89 53 18                mov    %edx,0x18(%ebx)
  61:   75 04                   jne    67 <acpi_suspend_lowlevel+0x67>

Ugly code (50 bytes):

  51:   b9 80 00 00 c0          mov    $0xc0000080,%ecx
  56:   0f 32                   rdmsr  
  58:   31 c9                   xor    %ecx,%ecx
  5a:   89 c6                   mov    %eax,%esi
  5c:   85 c9                   test   %ecx,%ecx
  5e:   89 45 ec                mov    %eax,-0x14(%ebp)
  61:   89 55 f0                mov    %edx,-0x10(%ebp)
  64:   89 73 14                mov    %esi,0x14(%ebx)
  67:   89 53 18                mov    %edx,0x18(%ebx)
  6a:   75 1b                   jne    87 <acpi_suspend_lowlevel+0x87>
  6c:   8b 75 ec                mov    -0x14(%ebp),%esi
  6f:   b9 80 00 00 c0          mov    $0xc0000080,%ecx
  74:   8b 7d f0                mov    -0x10(%ebp),%edi
  77:   89 f0                   mov    %esi,%eax
  79:   89 fa                   mov    %edi,%edx
  7b:   0f 30                   wrmsr  
  7d:   31 c0                   xor    %eax,%eax
  7f:   85 c0                   test   %eax,%eax
  81:   75 04                   jne    87 <acpi_suspend_lowlevel+0x87>
maintainer
#3 lore raw
Quoted 18 lines · George Spelvin
Quoted 3 lines · Borislav Petkov

I don't think that matters because this is called only once on suspend. Unless the cleaner assembly translates into a palpable speedup, which I doubt.

I was thinking about code *size*, actually; I agree that speed is too small to measure.

Clean code (21 bytes):

  4e:   b9 80 00 00 c0          mov    $0xc0000080,%ecx
  53:   0f 32                   rdmsr  
  55:   0f 30                   wrmsr  
  57:   31 f6                   xor    %esi,%esi
  59:   85 f6                   test   %esi,%esi
  5b:   89 43 14                mov    %eax,0x14(%ebx)
  5e:   89 53 18                mov    %edx,0x18(%ebx)
  61:   75 04                   jne    67 <acpi_suspend_lowlevel+0x67>
Quoted 1 line

Ugly code (50 bytes):

Right, that would matter maybe partially if the code was executed very often. In that case, the probability of it fitting in one cacheline is higher depending on alignment, and, you'd possibly save yourself loading a second cacheline.

If it is 29 bytes bigger, than we have a higher probability for using a second cacheline.

But again, I highly doubt even that would be noticeable. Especially on modern uarches with very aggressive and smart branch prediction.

And since this is being called only once, you won't notice the difference even with perf and specific instruction cache counters enabled.

But what do I know - I'm always open to surprising workloads! :-)

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#4 lore raw
In reply to George Spelvin
Quoted 5 lines · George Spelvin

It's marginal with only two call sites, but would it be worth factoring out the write-back function? Something like this (untested) patch. It definitely makes the generated assembly cleaner.

(Signed-off-by: George Spelvin <[email protected]> if you want it.)

Two call sites, statistically "never" executed, really doesn't justify adding a new assembly function. I was considering making a C wrapper, though... I would also like to change these to using rdmsrl/wrmsrl.

-hpa