x86/pat: Simplify the PAT programming protocol
authorKirill A. Shutemov <kirill.shutemov@linux.intel.com>
Wed, 24 Jan 2024 13:06:50 +0000 (15:06 +0200)
committerBorislav Petkov (AMD) <bp@alien8.de>
Tue, 20 Feb 2024 13:40:51 +0000 (14:40 +0100)
The programming protocol for the PAT MSR follows the MTRR programming
protocol. However, this protocol is cumbersome and requires disabling
caching (CR0.CD=1), which is not possible on some platforms.

Specifically, a TDX guest is not allowed to set CR0.CD. It triggers
a #VE exception.

It turns out that the requirement to follow the MTRR programming
protocol for PAT programming is unnecessarily strict. The new Intel
Software Developer Manual (http://www.intel.com/sdm) (December 2023)
relaxes this requirement, please refer to the section titled
"Programming the PAT" for more information.

In short, this section provides an alternative PAT update sequence which
doesn't need to disable caches around the PAT update but only to flush
those caches and TLBs.

The AMD documentation does not link PAT programming to MTRR and is there
fore, fine too.

The kernel only needs to flush the TLB after updating the PAT MSR. The
set_memory code already takes care of flushing the TLB and cache when
changing the memory type of a page.

  [ bp: Expand commit message. ]

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Juergen Gross <jgross@suse.com>
Link: https://lore.kernel.org/r/20240124130650.496056-1-kirill.shutemov@linux.intel.com
arch/x86/kernel/cpu/cacheinfo.c
arch/x86/mm/pat/memtype.c

index c131c412db89c58e820cdfa6758c4db6b59d3429..78afad50a7c0c7a5e0c44f4de46de4288a03ffec 100644 (file)
@@ -1118,15 +1118,16 @@ static void cache_cpu_init(void)
        unsigned long flags;
 
        local_irq_save(flags);
-       cache_disable();
 
-       if (memory_caching_control & CACHE_MTRR)
+       if (memory_caching_control & CACHE_MTRR) {
+               cache_disable();
                mtrr_generic_set_state();
+               cache_enable();
+       }
 
        if (memory_caching_control & CACHE_PAT)
                pat_cpu_init();
 
-       cache_enable();
        local_irq_restore(flags);
 }
 
index 0904d7e8e12608d231851a3bbcab788ddbdd2723..0d72183b5dd028ad83b98b38183af643ad3b21b5 100644 (file)
@@ -240,6 +240,8 @@ void pat_cpu_init(void)
        }
 
        wrmsrl(MSR_IA32_CR_PAT, pat_msr_val);
+
+       __flush_tlb_all();
 }
 
 /**
@@ -296,13 +298,8 @@ void __init pat_bp_init(void)
        /*
         * Xen PV doesn't allow to set PAT MSR, but all cache modes are
         * supported.
-        * When running as TDX guest setting the PAT MSR won't work either
-        * due to the requirement to set CR0.CD when doing so. Rely on
-        * firmware to have set the PAT MSR correctly.
         */
-       if (pat_disabled ||
-           cpu_feature_enabled(X86_FEATURE_XENPV) ||
-           cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
+       if (pat_disabled || cpu_feature_enabled(X86_FEATURE_XENPV)) {
                init_cache_modes(pat_msr_val);
                return;
        }