x86/mtrr, pat: make PAT independent from MTRR
Subsystems (3)
cover x86/mtrr, pat: make PAT independent from MTRR
Make PAT(Page Attribute Table) independent from
MTRR(Memory Type Range Register).
Some environments (mainly virtual ones) support only PAT, but not MTRR because PAT replaces MTRR.
It's tricky and no gain to support both MTRR and PAT except compatibility. So some VM technologies don't support MTRR, but only PAT.
This patch series makes PAT available on such environments without MTRR.
patch 1 and 2 are only preparation. no logic change, function rename (mtrr_ => mtrr_pat_ which is commonly used by both MTRR and PAT) and moving functions out from mtrr specific files to a common file. patch 3 is an essential patch which makes PAT independent from MTRR.
Isaku Yamahata (3):
x86/mtrr: split common funcs from mtrr.c
x86/mtrr: split common funcs from generic.c
x86/mtrr, pat: make PAT independent from MTRR
arch/x86/Kconfig | 1 - arch/x86/include/asm/mtrr.h | 37 ++- arch/x86/include/asm/pat.h | 2 + arch/x86/kernel/cpu/common.c | 2 +- arch/x86/kernel/cpu/mtrr/Makefile | 2 +- arch/x86/kernel/cpu/mtrr/generic.c | 116 +-------- arch/x86/kernel/cpu/mtrr/mtrr.c | 211 +---------------- arch/x86/kernel/cpu/mtrr/mtrr.h | 8 +- arch/x86/kernel/cpu/mtrr/rendezvous.c | 324 ++++++++++++++++++++++++++ arch/x86/kernel/setup.c | 4 +- arch/x86/kernel/smpboot.c | 8 +- arch/x86/mm/Makefile | 3 + arch/x86/mm/pat.c | 99 +++++++- arch/x86/power/cpu.c | 2 +- 14 files changed, 479 insertions(+), 340 deletions(-) create mode 100644 arch/x86/kernel/cpu/mtrr/rendezvous.c
Signature · 1 line
2.17.1
PATCH 1/3 x86/mtrr: split common funcs from mtrr.c
This is a preparation for make PAT(Page Attribute Table) independent from MTRR(Memory Type Range Register).
It renames prefix of common functions in mtrr.c from mtrr_ to mtrr_pat_ which are commonly used by both MTRR and PAT and moves out them from mtrr.c to rendezvous.c.
Only prefix rename and movement, no logic change.
--- arch/x86/include/asm/mtrr.h | 25 +-- arch/x86/kernel/cpu/common.c | 2 +- arch/x86/kernel/cpu/mtrr/Makefile | 2 +- arch/x86/kernel/cpu/mtrr/mtrr.c | 201 ++--------------------- arch/x86/kernel/cpu/mtrr/mtrr.h | 6 + arch/x86/kernel/cpu/mtrr/rendezvous.c | 221 ++++++++++++++++++++++++++ arch/x86/kernel/setup.c | 4 +- arch/x86/kernel/smpboot.c | 8 +- arch/x86/power/cpu.c | 2 +- 9 files changed, 260 insertions(+), 211 deletions(-) create mode 100644 arch/x86/kernel/cpu/mtrr/rendezvous.c
arch/x86/include/asm/mtrr.h +15 -10
@@ -32,6 +32,7 @@* arch_phys_wc_add and arch_phys_wc_del. */ # ifdef CONFIG_MTRR +extern bool mtrr_enabled (void );extern u8 mtrr_type_lookup (u64 addr,u64 end,u8 *uniform);extern void mtrr_save_fixed_ranges (void *);extern void mtrr_save_state (void );
@@ -42,14 +43,18 @@ extern int mtrr_add_page(unsigned long base, unsigned long size,extern int mtrr_del (int reg,unsigned long base,unsigned long size);extern int mtrr_del_page (int reg,unsigned long base,unsigned long size);extern void mtrr_centaur_report_mcr (int mcr,u32 lo,u32 hi); -extern void mtrr_ap_init (void ); -extern void mtrr_bp_init (void ); -extern void set_mtrr_aps_delayed_init (void ); -extern void mtrr_aps_init (void ); -extern void mtrr_bp_restore (void ); +extern void mtrr_pat_ap_init (void ); +extern void mtrr_pat_bp_init (void ); +extern void set_mtrr_pat_aps_delayed_init (void ); +extern void mtrr_pat_aps_init (void ); +extern void mtrr_pat_bp_restore (void );extern int mtrr_trim_uncached_memory (unsigned long end_pfn);extern int amd_special_default_mtrr (void );# else +static inline bool mtrr_enabled (void ) +{ +return false; +}static inline u8 mtrr_type_lookup (u64 addr,u64 end,u8 *uniform) {/*
@@ -84,15 +89,15 @@ static inline int mtrr_trim_uncached_memory(unsigned long end_pfn)static inline void mtrr_centaur_report_mcr (int mcr,u32 lo,u32 hi) { } -static inline void mtrr_bp_init (void ) +static inline void mtrr_pat_bp_init (void ) {pat_disable ("MTRRs disabled, skipping PAT initialization too." ); } -#define mtrr_ap_init () do {} while (0) -#define set_mtrr_aps_delayed_init () do {} while (0) -#define mtrr_aps_init () do {} while (0) -#define mtrr_bp_restore () do {} while (0) +static inline void mtrr_pat_ap_init (void ) { }; +static inline void set_mtrr_pat_aps_delayed_init (void ) { }; +static inline void mtrr_pat_aps_init (void ) { }; +static inline void mtrr_pat_bp_restore (void ) { };# endif #ifdef CONFIG_COMPAT
arch/x86/kernel/cpu/common.c +1 -1
@@ -1550,7 +1550,7 @@ void identify_secondary_cpu(struct cpuinfo_x86 *c)#ifdef CONFIG_X86_32 enable_sep_cpu ();#endif -mtrr_ap_init (); +mtrr_pat_ap_init ();validate_apic_and_package_id (c);x86_spec_ctrl_setup_ap (); }
arch/x86/kernel/cpu/mtrr/Makefile +1 -1
@@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y := mtrr.o if.o generic.o cleanup.o +obj-y := mtrr.o if.o generic.o cleanup.o rendezvous.o obj-$(CONFIG_X86_32) += amd.o cyrix.o centaur.o
arch/x86/kernel/cpu/mtrr/mtrr.c +9 -192
290 lines not on this page · raw message
arch/x86/kernel/cpu/mtrr/mtrr.h +6 -0
@@ -71,6 +71,12 @@ void mtrr_state_warn(void);const char *mtrr_attrib_to_str (int x);void mtrr_wrmsr (unsigned ,unsigned ,unsigned ); +/* rendezvous */ +void set_mtrr_pat (unsigned int reg,unsigned long base, +unsigned long size,mtrr_type type); +void set_mtrr_pat_cpuslocked (unsigned int reg,unsigned long base, +unsigned long size,mtrr_type type); +/* CPU specific mtrr init functions */ int amd_init_mtrr (void );int cyrix_init_mtrr (void );
arch/x86/kernel/cpu/mtrr/rendezvous.c new file +221 -0
221 lines not on this page · raw message
arch/x86/kernel/setup.c +2 -2
@@ -1064,7 +1064,7 @@ void __init setup_arch(char **cmdline_p) max_pfn =e820__end_of_ram_pfn ();/* update e820 for memory not covered by WB MTRRs */ -mtrr_bp_init (); +mtrr_pat_bp_init ();if (mtrr_trim_uncached_memory (max_pfn)) max_pfn =e820__end_of_ram_pfn ();
@@ -1072,7 +1072,7 @@ void __init setup_arch(char **cmdline_p)/* * This call is required when the CPU does not support PAT. If -* mtrr_bp_init() invoked it already via pat_init() the call has no +* mtrr_pat_bp_init() invoked it already via pat_init() the call has no * effect. */ init_cache_modes ();
arch/x86/kernel/smpboot.c +4 -4
@@ -1370,7 +1370,7 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)uv_system_init (); -set_mtrr_aps_delayed_init (); +set_mtrr_pat_aps_delayed_init ();smp_quirk_init_udelay ();
@@ -1379,12 +1379,12 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)void arch_enable_nonboot_cpus_begin (void ) { -set_mtrr_aps_delayed_init (); +set_mtrr_pat_aps_delayed_init (); }void arch_enable_nonboot_cpus_end (void ) { -mtrr_aps_init (); +mtrr_pat_aps_init (); }/*
@@ -1424,7 +1424,7 @@ void __init native_smp_cpus_done(unsigned int max_cpus)nmi_selftest ();impress_friends (); -mtrr_aps_init (); +mtrr_pat_aps_init (); }static int __initdata setup_possible_cpus =-1 ;
arch/x86/power/cpu.c +1 -1
@@ -263,7 +263,7 @@ static void notrace __restore_processor_state(struct saved_context *ctxt)do_fpu_end ();tsc_verify_tsc_adjust (true); x86_platform.restore_sched_clock_state (); -mtrr_bp_restore (); +mtrr_pat_bp_restore ();perf_restore_debug_store ();msr_restore_context (ctxt); }
Signature · 1 line
2.17.1
PATCH 2/3 x86/mtrr: split common funcs from generic.c
In reply to Isaku YamahataThis is a preparation for make PAT(Page Attribute Table) independent from MTRR(Memory Type Range Register).
It renames prefix of common functions in mtrr/generic.c from mtrr_ to mtrr_pat_ which are commonly used by both MTRR and PAT and moves out them from mtrr/generic.c to rendezvous.c.
Only prefix rename and movement, no logic change.
--- arch/x86/include/asm/mtrr.h | 4 + arch/x86/kernel/cpu/mtrr/generic.c | 111 ++------------------------ arch/x86/kernel/cpu/mtrr/mtrr.c | 2 +- arch/x86/kernel/cpu/mtrr/mtrr.h | 3 +- arch/x86/kernel/cpu/mtrr/rendezvous.c | 91 +++++++++++++++++++++ 5 files changed, 106 insertions(+), 105 deletions(-)
arch/x86/include/asm/mtrr.h +4 -0
@@ -33,6 +33,8 @@*/ # ifdef CONFIG_MTRR extern bool mtrr_enabled (void ); +extern void mtrr_pat_prepare_set(void) __acquires(set_atomicity_lock); +extern void mtrr_pat_post_set(void) __releases(set_atomicity_lock);extern u8 mtrr_type_lookup (u64 addr,u64 end,u8 *uniform);extern void mtrr_save_fixed_ranges (void *);extern void mtrr_save_state (void );
@@ -55,6 +57,8 @@ static inline bool mtrr_enabled(void) {return false; } +static inline void mtrr_pat_prepare_set (void ) { }; +static inline void mtrr_pat_post_set (void ) { };static inline u8 mtrr_type_lookup (u64 addr,u64 end,u8 *uniform) {/*
arch/x86/kernel/cpu/mtrr/generic.c +8 -103
166 lines not on this page · raw message
arch/x86/kernel/cpu/mtrr/mtrr.c +1 -1
@@ -646,7 +646,7 @@ void __init mtrr_pat_bp_init(void) __mtrr_enabled =get_mtrr_state ();if (mtrr_enabled ()) -mtrr_bp_pat_init (); +pat_bp_init ();if (mtrr_cleanup (phys_addr)) { changed_by_mtrr_cleanup =1 ;
arch/x86/kernel/cpu/mtrr/mtrr.h +2 -1
@@ -53,7 +53,7 @@ void set_mtrr_prepare_save(struct set_mtrr_context *ctxt);void fill_mtrr_var_range (unsigned int index,u32 base_lo,u32 base_hi,u32 mask_lo,u32 mask_hi);bool get_mtrr_state (void ); -void mtrr_bp_pat_init (void ); +void pat_bp_init (void );extern void __init set_mtrr_ops (const struct mtrr_ops *ops);
@@ -76,6 +76,7 @@ void set_mtrr_pat(unsigned int reg, unsigned long base, unsigned long size, mtrr_type type);void set_mtrr_pat_cpuslocked (unsigned int reg,unsigned long base,unsigned long size,mtrr_type type); +extern u32 mtrr_deftype_lo, mtrr_deftype_hi;/* CPU specific mtrr init functions */ int amd_init_mtrr (void );
arch/x86/kernel/cpu/mtrr/rendezvous.c +91 -0
105 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 3/3 x86/mtrr, pat: make PAT independent from MTRR
In reply to Isaku YamahataThis patch makes PAT(Page Attribute Table) independent from MTRR(Memory Type Range Register)
Some environments (mainly virtual ones) support only PAT, not MTRR. It's tricky and no gain to support both MTRR and PAT at the same time except compatibility because PAT replaces MTRR. So some VM technologies don't support MTRR, but only PAT. This patch make PAT available on such environments without MTRR.
--- arch/x86/Kconfig | 1 - arch/x86/include/asm/mtrr.h | 32 +++++---- arch/x86/include/asm/pat.h | 2 + arch/x86/kernel/cpu/mtrr/generic.c | 5 -- arch/x86/kernel/cpu/mtrr/mtrr.c | 8 +-- arch/x86/kernel/cpu/mtrr/mtrr.h | 1 - arch/x86/kernel/cpu/mtrr/rendezvous.c | 76 +++++++++++--------- arch/x86/mm/Makefile | 3 + arch/x86/mm/pat.c | 99 ++++++++++++++++++++++++--- 9 files changed, 158 insertions(+), 69 deletions(-)
arch/x86/Kconfig +0 -1
@@ -1838,7 +1838,6 @@ config MTRR_SANITIZER_SPARE_REG_NR_DEFAULT config X86_PAT def_bool y prompt "x86 PAT support" if EXPERT - depends on MTRR ---help--- Use PAT attributes to setup page level cache control.
arch/x86/include/asm/mtrr.h +18 -14
@@ -31,10 +31,25 @@* The following functions are for use by other drivers that cannot use * arch_phys_wc_add and arch_phys_wc_del. */ -# ifdef CONFIG_MTRR -extern bool mtrr_enabled (void ); +#if defined(CONFIG_MTRR ) || defined(CONFIG_X86_PAT ) +/* common method for MTRR and PAT */ extern void mtrr_pat_prepare_set(void) __acquires(set_atomicity_lock); extern void mtrr_pat_post_set(void) __releases(set_atomicity_lock); +extern void mtrr_pat_ap_init (void ); +extern void set_mtrr_pat_aps_delayed_init (void ); +extern void mtrr_pat_aps_init (void ); +extern void mtrr_pat_bp_restore (void ); +#else +static inline void mtrr_pat_prepare_set (void ) { } +static inline void mtrr_pat_post_set (void ) { } +static inline void mtrr_pat_ap_init (void ) { }; +static inline void set_mtrr_pat_aps_delayed_init (void ) { }; +static inline void mtrr_pat_aps_init (void ) { }; +static inline void mtrr_pat_bp_restore (void ) { }; +#endif + +# ifdef CONFIG_MTRR +extern bool mtrr_enabled (void );extern u8 mtrr_type_lookup (u64 addr,u64 end,u8 *uniform);extern void mtrr_save_fixed_ranges (void *);extern void mtrr_save_state (void );
@@ -45,11 +60,7 @@ extern int mtrr_add_page(unsigned long base, unsigned long size,extern int mtrr_del (int reg,unsigned long base,unsigned long size);extern int mtrr_del_page (int reg,unsigned long base,unsigned long size);extern void mtrr_centaur_report_mcr (int mcr,u32 lo,u32 hi); -extern void mtrr_pat_ap_init (void );extern void mtrr_pat_bp_init (void ); -extern void set_mtrr_pat_aps_delayed_init (void ); -extern void mtrr_pat_aps_init (void ); -extern void mtrr_pat_bp_restore (void );extern int mtrr_trim_uncached_memory (unsigned long end_pfn);extern int amd_special_default_mtrr (void );# else
@@ -57,8 +68,6 @@ static inline bool mtrr_enabled(void) {return false; } -static inline void mtrr_pat_prepare_set (void ) { }; -static inline void mtrr_pat_post_set (void ) { };static inline u8 mtrr_type_lookup (u64 addr,u64 end,u8 *uniform) {/*
@@ -95,13 +104,8 @@ static inline void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi) }static inline void mtrr_pat_bp_init (void ) { -pat_disable ("MTRRs disabled, skipping PAT initialization too." ); +pat_bp_init (); } - -static inline void mtrr_pat_ap_init (void ) { }; -static inline void set_mtrr_pat_aps_delayed_init (void ) { }; -static inline void mtrr_pat_aps_init (void ) { }; -static inline void mtrr_pat_bp_restore (void ) { };# endif #ifdef CONFIG_COMPAT
arch/x86/include/asm/pat.h +2 -0
@@ -7,7 +7,9 @@bool pat_enabled (void );void pat_disable (const char *reason); +extern void pat_set (void );extern void pat_init (void ); +extern void pat_bp_init (void );extern void init_cache_modes (void ); extern int reserve_memtype(u64 start, u64 end,
arch/x86/kernel/cpu/mtrr/generic.c +0 -5
@@ -6,13 +6,8 @@#define DEBUG #include <linux/export.h> -#include <linux/init.h> -#include <linux/io.h> #include <linux/mm.h> -#include <asm/processor-flags.h> -#include <asm/cpufeature.h> -#include <asm/tlbflush.h> #include <asm/mtrr.h> #include <asm/msr.h> #include <asm/pat.h>
arch/x86/kernel/cpu/mtrr/mtrr.c +1 -7
@@ -657,13 +657,7 @@ void __init mtrr_pat_bp_init(void)if (!mtrr_enabled ()) {pr_info ("Disabled\n" ); - -/* -* PAT initialization relies on MTRR's rendezvous handler. -* Skip PAT init until the handler can initialize both -* features independently. -*/ -pat_disable ("MTRRs disabled, skipping PAT initialization too." ); +pat_bp_init (); } }
arch/x86/kernel/cpu/mtrr/mtrr.h +0 -1
@@ -53,7 +53,6 @@ void set_mtrr_prepare_save(struct set_mtrr_context *ctxt);void fill_mtrr_var_range (unsigned int index,u32 base_lo,u32 base_hi,u32 mask_lo,u32 mask_hi);bool get_mtrr_state (void ); -void pat_bp_init (void );extern void __init set_mtrr_ops (const struct mtrr_ops *ops);
arch/x86/kernel/cpu/mtrr/rendezvous.c +44 -32
148 lines not on this page · raw message
arch/x86/mm/Makefile +3 -0
@@ -24,6 +24,9 @@ CFLAGS_mem_encrypt_identity.o := $(nostackp) CFLAGS_fault.o := -I $(srctree)/$(src)/../include/asm/trace obj-$(CONFIG_X86_PAT) += pat_rbtree.o +ifndef CONFIG_MTRR +obj-$(CONFIG_X86_PAT) += ../kernel/cpu/mtrr/rendezvous.o +endif obj-$(CONFIG_X86_32) += pgtable_32.o iomap_32.o
arch/x86/mm/pat.c +90 -9
143 lines not on this page · raw message
Signature · 1 line
2.17.1
Borislav Petkov
maintainerQuoted 7 lines · Isaku Yamahata
Make PAT(Page Attribute Table) independent from
MTRR(Memory Type Range Register).
Some environments (mainly virtual ones) support only PAT, but not MTRR because PAT replaces MTRR.
It's tricky and no gain to support both MTRR and PAT except compatibility. So some VM technologies don't support MTRR, but only PAT.
This patch series makes PAT available on such environments without MTRR.
And this "justification" is not even trying. Which "VM technologies" are those? Why do we care? What's the impact? Why do we want this?
You need to sell this properly.
Also, your patches are huge. You'd need to split them sensibly.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Quoted 7 lines · Borislav Petkov
Quoted 6 lines · Isaku Yamahata
Make PAT(Page Attribute Table) independent from
MTRR(Memory Type Range Register).
Some environments (mainly virtual ones) support only PAT, but not MTRR because PAT replaces MTRR.
It's tricky and no gain to support both MTRR and PAT except compatibility. So some VM technologies don't support MTRR, but only PAT.
I do not think it is technically correct on bare metal. AFAIK, MTRR is still the only way to setup cache attribute in real-mode, which BIOS SMI handler relies on in SMM.
Quoted 6 lines
Quoted 1 line
This patch series makes PAT available on such environments without MTRR.
And this "justification" is not even trying. Which "VM technologies" are those? Why do we care? What's the impact? Why do we want this?
You need to sell this properly.
Agreed. If the situation is still the same, Xen does not support MTRR, and the kernel sets the PAT table to the BIOS hand-off state when MTRR is disabled. The change below accommodated the fact that Xen hypervisor enables WC before hand-off, which is different from the default BIOS hand-off state. The kernel does not support setting PAT when MTRR is disabled due to the dependency Isaku mentioned.
https://www.mail-archive.com/[email protected]/msg1107094.html
-Toshi
Quoted 9 lines · Toshi
Quoted 7 lines · Borislav Petkov
Quoted 6 lines · Isaku Yamahata
Make PAT(Page Attribute Table) independent from
MTRR(Memory Type Range Register).
Some environments (mainly virtual ones) support only PAT, but not MTRR because PAT replaces MTRR.
It's tricky and no gain to support both MTRR and PAT except compatibility. So some VM technologies don't support MTRR, but only PAT.
Quoted 3 lines
I do not think it is technically correct on bare metal. AFAIK, MTRR is still the only way to setup cache attribute in real-mode, which BIOS SMI handler relies on in SMM.
Then you're claiming if it's baremetal, both MTRR and PAT should be enabled/disabled at the same time?
Quoted 15 lines
Quoted 6 lines
Quoted 1 line
This patch series makes PAT available on such environments without MTRR.
And this "justification" is not even trying. Which "VM technologies" are those? Why do we care? What's the impact? Why do we want this?
You need to sell this properly.
Agreed. If the situation is still the same, Xen does not support MTRR, and the kernel sets the PAT table to the BIOS hand-off state when MTRR is disabled. The change below accommodated the fact that Xen hypervisor enables WC before hand-off, which is different from the default BIOS hand-off state. The kernel does not support setting PAT when MTRR is disabled due to the dependency Isaku mentioned.
Thanks for supplement.
In addition to Xen, KVM+qemu can enable/disable MTRR, PAT independently. So user may want to disable MTRR to reduce attack surface. ACRN doesn't support MTRR.
Let me include those description for next respin.
Signature · 1 line
Isaku Yamahata <[email protected]>
Borislav Petkov
maintainerQuoted 2 lines · Isaku Yamahata
In addition to Xen, KVM+qemu can enable/disable MTRR, PAT independently. So user may want to disable MTRR to reduce attack surface.
No, no "user may want" etc vague formulations. Just because some virt thing "can" do stuff doesn't mean we should change the kernel. What are the clear benefits of your proposal, why should it go upstream and why should it be exposed to everybody?
How is going to be used it and what would it bring?
Are there any downsides?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Quoted 12 lines · Isaku Yamahata
Quoted 9 lines · Toshi
Quoted 7 lines · Borislav Petkov
Quoted 6 lines · Isaku Yamahata
Make PAT(Page Attribute Table) independent from
MTRR(Memory Type Range Register).
Some environments (mainly virtual ones) support only PAT, but not MTRR because PAT replaces MTRR.
It's tricky and no gain to support both MTRR and PAT except compatibility. So some VM technologies don't support MTRR, but only PAT.
Quoted 6 lines
Quoted 3 lines
I do not think it is technically correct on bare metal. AFAIK, MTRR is still the only way to setup cache attribute in real-mode, which BIOS SMI handler relies on in SMM.
Then you're claiming if it's baremetal, both MTRR and PAT should be enabled/disabled at the same time?
No, I did not say that. My point:
- Your statement of MTTR being useless is not correct. It's still used. The OS should leave the MTTR hand-off state.
I agree with you in general that PAT and MTTR init should be independent. However, as Boris said, please verify the impact of your change. As I mentioned in the Xen's example, hypervisor may have non- default PAT hand-off setting.
Thanks,
-Toshi