[PATCH v4 05/16] x86/mtrr: split MTRR specific handling from cache dis/enabling

Juergen Gross posted 16 patches 3 years, 6 months ago
[PATCH v4 05/16] x86/mtrr: split MTRR specific handling from cache dis/enabling
Posted by Juergen Gross 3 years, 6 months ago
Split the MTRR specific actions from cache_disable() and cache_enable()
into the new functions mtrr_disable() and mtrr_enable().

Signed-off-by: Juergen Gross <jgross@suse.com>
---
V4:
- carved out from other patch (Borislav Petkov)
---
 arch/x86/include/asm/mtrr.h        |  4 ++++
 arch/x86/kernel/cpu/mtrr/generic.c | 26 +++++++++++++++++++-------
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/arch/x86/include/asm/mtrr.h b/arch/x86/include/asm/mtrr.h
index 76d726074c16..12a16caed395 100644
--- a/arch/x86/include/asm/mtrr.h
+++ b/arch/x86/include/asm/mtrr.h
@@ -48,6 +48,8 @@ extern void mtrr_aps_init(void);
 extern void mtrr_bp_restore(void);
 extern int mtrr_trim_uncached_memory(unsigned long end_pfn);
 extern int amd_special_default_mtrr(void);
+void mtrr_disable(void);
+void mtrr_enable(void);
 #  else
 static inline u8 mtrr_type_lookup(u64 addr, u64 end, u8 *uniform)
 {
@@ -87,6 +89,8 @@ static inline void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi)
 #define set_mtrr_aps_delayed_init() do {} while (0)
 #define mtrr_aps_init() do {} while (0)
 #define mtrr_bp_restore() do {} while (0)
+#define mtrr_disable() do {} while (0)
+#define mtrr_enable() do {} while (0)
 #  endif
 
 #ifdef CONFIG_COMPAT
diff --git a/arch/x86/kernel/cpu/mtrr/generic.c b/arch/x86/kernel/cpu/mtrr/generic.c
index aebdc90a2489..164d753e9867 100644
--- a/arch/x86/kernel/cpu/mtrr/generic.c
+++ b/arch/x86/kernel/cpu/mtrr/generic.c
@@ -716,6 +716,21 @@ static unsigned long set_mtrr_state(void)
 	return change_mask;
 }
 
+void mtrr_disable(void)
+{
+	/* Save MTRR state */
+	rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
+
+	/* Disable MTRRs, and set the default type to uncached */
+	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
+}
+
+void mtrr_enable(void)
+{
+	/* Intel (P6) standard MTRRs */
+	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
+}
+
 /*
  * Disable and enable caches. Needed for changing MTRRs and the PAT MSR.
  *
@@ -764,11 +779,8 @@ void cache_disable(void) __acquires(cache_disable_lock)
 	count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
 	flush_tlb_local();
 
-	/* Save MTRR state */
-	rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
-
-	/* Disable MTRRs, and set the default type to uncached */
-	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
+	if (boot_cpu_has(X86_FEATURE_MTRR))
+		mtrr_disable();
 
 	/* Again, only flush caches if we have to. */
 	if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
@@ -781,8 +793,8 @@ void cache_enable(void) __releases(cache_disable_lock)
 	count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
 	flush_tlb_local();
 
-	/* Intel (P6) standard MTRRs */
-	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
+	if (boot_cpu_has(X86_FEATURE_MTRR))
+		mtrr_enable();
 
 	/* Enable caches */
 	write_cr0(read_cr0() & ~X86_CR0_CD);
-- 
2.35.3
Re: [PATCH v4 05/16] x86/mtrr: split MTRR specific handling from cache dis/enabling
Posted by Borislav Petkov 3 years, 5 months ago
On Tue, Oct 04, 2022 at 10:10:12AM +0200, Juergen Gross wrote:
> @@ -764,11 +779,8 @@ void cache_disable(void) __acquires(cache_disable_lock)
>  	count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
>  	flush_tlb_local();
>  
> -	/* Save MTRR state */
> -	rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
> -
> -	/* Disable MTRRs, and set the default type to uncached */
> -	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
> +	if (boot_cpu_has(X86_FEATURE_MTRR))

check_for_deprecated_apis: Warning: arch/x86/kernel/cpu/mtrr/generic.c:782: Do not use boot_cpu_has() - use cpu_feature_enabled() instead.

> +		mtrr_disable();
>  
>  	/* Again, only flush caches if we have to. */
>  	if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
> @@ -781,8 +793,8 @@ void cache_enable(void) __releases(cache_disable_lock)
>  	count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
>  	flush_tlb_local();
>  
> -	/* Intel (P6) standard MTRRs */
> -	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
> +	if (boot_cpu_has(X86_FEATURE_MTRR))

Ditto.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH v4 05/16] x86/mtrr: split MTRR specific handling from cache dis/enabling
Posted by Juergen Gross 3 years, 5 months ago
On 26.10.22 11:24, Borislav Petkov wrote:
> On Tue, Oct 04, 2022 at 10:10:12AM +0200, Juergen Gross wrote:
>> @@ -764,11 +779,8 @@ void cache_disable(void) __acquires(cache_disable_lock)
>>   	count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
>>   	flush_tlb_local();
>>   
>> -	/* Save MTRR state */
>> -	rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
>> -
>> -	/* Disable MTRRs, and set the default type to uncached */
>> -	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
>> +	if (boot_cpu_has(X86_FEATURE_MTRR))
> 
> check_for_deprecated_apis: Warning: arch/x86/kernel/cpu/mtrr/generic.c:782: Do not use boot_cpu_has() - use cpu_feature_enabled() instead.

Okay.


Juergen