Skip to content

Commit ffc92cf

Browse files
kirylbp3tk0v
authored andcommitted
x86/pat: Simplify the PAT programming protocol
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
1 parent b401b62 commit ffc92cf

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

arch/x86/kernel/cpu/cacheinfo.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,15 +1118,16 @@ static void cache_cpu_init(void)
11181118
unsigned long flags;
11191119

11201120
local_irq_save(flags);
1121-
cache_disable();
11221121

1123-
if (memory_caching_control & CACHE_MTRR)
1122+
if (memory_caching_control & CACHE_MTRR) {
1123+
cache_disable();
11241124
mtrr_generic_set_state();
1125+
cache_enable();
1126+
}
11251127

11261128
if (memory_caching_control & CACHE_PAT)
11271129
pat_cpu_init();
11281130

1129-
cache_enable();
11301131
local_irq_restore(flags);
11311132
}
11321133

arch/x86/mm/pat/memtype.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ void pat_cpu_init(void)
240240
}
241241

242242
wrmsrl(MSR_IA32_CR_PAT, pat_msr_val);
243+
244+
__flush_tlb_all();
243245
}
244246

245247
/**
@@ -296,13 +298,8 @@ void __init pat_bp_init(void)
296298
/*
297299
* Xen PV doesn't allow to set PAT MSR, but all cache modes are
298300
* supported.
299-
* When running as TDX guest setting the PAT MSR won't work either
300-
* due to the requirement to set CR0.CD when doing so. Rely on
301-
* firmware to have set the PAT MSR correctly.
302301
*/
303-
if (pat_disabled ||
304-
cpu_feature_enabled(X86_FEATURE_XENPV) ||
305-
cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
302+
if (pat_disabled || cpu_feature_enabled(X86_FEATURE_XENPV)) {
306303
init_cache_modes(pat_msr_val);
307304
return;
308305
}

0 commit comments

Comments
 (0)