x86: make pat and mtrr independent from each other
Subsystems (4)
v2 cover x86: make pat and mtrr independent from each other
Today PAT can't be used without MTRR being available, unless MTRR is at least configured via CONFIG_MTRR and the system is running as Xen PV guest. In this case PAT is automatically available via the hypervisor, but the PAT MSR can't be modified by the kernel and MTRR is disabled.
The same applies to a kernel built with no MTRR support: it won't allow to use the PAT MSR, even if there is no technical reason for that, other than setting up PAT on all cpus the same way (which is a requirement of the processor's cache management) is relying on some MTRR specific code.
Fix all of that by:
- moving the function needed by PAT from MTRR specific code one level up
- reworking the init sequences of MTRR and PAT to be more similar to each other without calling PAT from MTRR code
- removing the dependency of PAT on MTRR
While working on that I discovered two minor bugs in MTRR code, which are fixed, too.
Changes in V2:
- complete rework of the patches based on comments by Boris Petkov
- added several patches to the series
Juergen Gross (10):
x86/mtrr: fix MTRR fixup on APs
x86/mtrr: remove unused cyrix_set_all() function x86/mtrr: replace use_intel() with a local flag
x86: move some code out of arch/x86/kernel/cpu/mtrr x86/mtrr: split generic_set_all()
x86/mtrr: remove set_all callback from struct mtrr_ops x86/mtrr: simplify mtrr_bp_init()
x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init x86/mtrr: add a stop_machine() handler calling only cache_cpu_init()
x86: decouple pat and mtrr handling
arch/x86/include/asm/cacheinfo.h | 14 +++ arch/x86/include/asm/memtype.h | 5 +- arch/x86/include/asm/mtrr.h | 12 +-- arch/x86/kernel/cpu/cacheinfo.c | 159 +++++++++++++++++++++++++++++ arch/x86/kernel/cpu/common.c | 3 +- arch/x86/kernel/cpu/mtrr/cyrix.c | 34 ------ arch/x86/kernel/cpu/mtrr/generic.c | 120 ++++------------------ arch/x86/kernel/cpu/mtrr/mtrr.c | 158 +++++----------------------- arch/x86/kernel/cpu/mtrr/mtrr.h | 5 - arch/x86/kernel/setup.c | 14 +-- arch/x86/kernel/smpboot.c | 9 +- arch/x86/mm/pat/memtype.c | 127 +++++++---------------- arch/x86/power/cpu.c | 3 +- 13 files changed, 274 insertions(+), 389 deletions(-)
Signature · 1 line
2.35.3
PATCH v2 1/10 x86/mtrr: fix MTRR fixup on APs
When booting or resuming the system MTRR state is saved on the boot processor and then this state is loaded into MTRRs of all other cpus. During update of the MTRRs the MTRR mechanism needs to be disabled by writing the related MSR. The old contents of this MSR are saved in a set of static variables and later those static variables are used to restore the MSR.
In case the MSR contents need to be modified on a cpu due to the MSR not having been initialized properly by the BIOS, the related update function is modifying the static variables accordingly.
Unfortunately the MTRR state update is usually running on all cpus at the same time, so using just one set of static variables for all cpus is racy in case the MSR contents differ across cpus.
Fix that by using percpu variables for saving the MSR contents.
---
I thought adding a "Fixes:" tag for the kernel's initial git commit would maybe be entertaining, but without being really helpful. The percpu variables were preferred over on-stack ones in order to avoid more code churn in followup patches decoupling PAT from MTRR support.
V2:
- new patch
--- arch/x86/kernel/cpu/mtrr/generic.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
arch/x86/kernel/cpu/mtrr/generic.c +14 -7
@@ -679,7 +679,8 @@ static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr)return changed; } -static u32 deftype_lo, deftype_hi; +static DEFINE_PER_CPU(u32, deftype_lo); +static DEFINE_PER_CPU(u32, deftype_hi);/** * set_mtrr_state - Set the MTRR state for this CPU.
@@ -691,6 +692,7 @@ static unsigned long set_mtrr_state(void) {unsigned long change_mask =0 ;unsigned int i; +u32 *lo =this_cpu_ptr (&deftype_lo);for (i =0 ; i < num_var_ranges; i++) {if (set_mtrr_var_ranges (i, &mtrr_state.var_ranges[i]))
@@ -704,10 +706,10 @@ static unsigned long set_mtrr_state(void)* Set_mtrr_restore restores the old value of MTRRdefType, * so to set it we fiddle with the saved value: */ -if ((deftype_lo &0xff ) != mtrr_state.def_type - || ((deftype_lo &0xc00 ) >>10 ) != mtrr_state.enabled) { +if ((*lo &0xff ) != mtrr_state.def_type + || ((*lo &0xc00 ) >>10 ) != mtrr_state.enabled) { - deftype_lo = (deftype_lo & ~0xcff ) | mtrr_state.def_type | + *lo = (*lo & ~0xcff ) | mtrr_state.def_type | (mtrr_state.enabled <<10 ); change_mask |=MTRR_CHANGE_MASK_DEFTYPE ; }
@@ -729,6 +731,8 @@ static DEFINE_RAW_SPINLOCK(set_atomicity_lock);static void prepare_set (void )__acquires (set_atomicity_lock) {unsigned long cr0; +u32 *lo =this_cpu_ptr (&deftype_lo); +u32 *hi =this_cpu_ptr (&deftype_hi);/* * Note that this is not ideal
@@ -763,10 +767,10 @@ static void prepare_set(void) __acquires(set_atomicity_lock)flush_tlb_local ();/* Save MTRR state */ -rdmsr (MSR_MTRRdefType, deftype_lo, deftype_hi); +rdmsr (MSR_MTRRdefType, *lo, *hi);/* Disable MTRRs, and set the default type to uncached */ -mtrr_wrmsr (MSR_MTRRdefType, deftype_lo & ~0xcff , deftype_hi); +mtrr_wrmsr (MSR_MTRRdefType, *lo & ~0xcff , *hi);/* Again, only flush caches if we have to. */ if (!static_cpu_has (X86_FEATURE_SELFSNOOP ))
@@ -775,12 +779,15 @@ static void prepare_set(void) __acquires(set_atomicity_lock)static void post_set (void )__releases (set_atomicity_lock) { +u32 *lo =this_cpu_ptr (&deftype_lo); +u32 *hi =this_cpu_ptr (&deftype_hi); +/* Flush TLBs (no need to flush caches - they are disabled) */ count_vm_tlb_event (NR_TLB_LOCAL_FLUSH_ALL );flush_tlb_local ();/* Intel (P6) standard MTRRs */ -mtrr_wrmsr (MSR_MTRRdefType, deftype_lo, deftype_hi); +mtrr_wrmsr (MSR_MTRRdefType, *lo, *hi);/* Enable caches */ write_cr0 (read_cr0 () & ~X86_CR0_CD );
Signature · 1 line
2.35.3
Greg KH
maintainerRe: [PATCH v2 01/10] x86/mtrr: fix MTRR fixup on APs
Quoted 15 lines · Juergen Gross
When booting or resuming the system MTRR state is saved on the boot processor and then this state is loaded into MTRRs of all other cpus. During update of the MTRRs the MTRR mechanism needs to be disabled by writing the related MSR. The old contents of this MSR are saved in a set of static variables and later those static variables are used to restore the MSR.
In case the MSR contents need to be modified on a cpu due to the MSR not having been initialized properly by the BIOS, the related update function is modifying the static variables accordingly.
Unfortunately the MTRR state update is usually running on all cpus at the same time, so using just one set of static variables for all cpus is racy in case the MSR contents differ across cpus.
Quoted 7 lines
Fix that by using percpu variables for saving the MSR contents.
Signed-off-by Juergen Gross <[email protected]>---
I thought adding a "Fixes:" tag for the kernel's initial git commit would maybe be entertaining, but without being really helpful.
So that means I will just do a "best guess" as to how far to backport things. Hopefully I guess well...
thanks,
greg k-h
Borislav Petkov
maintainerRe: [PATCH v2 01/10] x86/mtrr: fix MTRR fixup on APs
In reply to Juergen GrossQuoted 2 lines · Juergen Gross
When booting or resuming the system MTRR state is saved on the boot processor and then this state is loaded into MTRRs of all other cpus.
s/cpu/CPU/g
Pls check all commit messages.
Quoted 15 lines
During update of the MTRRs the MTRR mechanism needs to be disabled by writing the related MSR. The old contents of this MSR are saved in a set of static variables and later those static variables are used to restore the MSR.
In case the MSR contents need to be modified on a cpu due to the MSR not having been initialized properly by the BIOS, the related update function is modifying the static variables accordingly.
Unfortunately the MTRR state update is usually running on all cpus at the same time, so using just one set of static variables for all cpus is racy in case the MSR contents differ across cpus.
Fix that by using percpu variables for saving the MSR contents.
Quoted 8 lines
Signed-off-by Juergen Gross <[email protected]>---
I thought adding a "Fixes:" tag for the kernel's initial git commit would maybe be entertaining, but without being really helpful. The percpu variables were preferred over on-stack ones in order to avoid more code churn in followup patches decoupling PAT from MTRR support.
So if that thing has been broken for so long and no one noticed, we could just as well not backport to stable at all...
Quoted 11 lines
V2:
- new patch--- arch/x86/kernel/cpu/mtrr/generic.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)arch/x86/kernel/cpu/mtrr/generic.c +0
-0
arch/x86/kernel/cpu/mtrr/generic.c static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr) · line 683
Quoted 6 lines
arch/x86/kernel/cpu/mtrr/generic.c +2
-1@@ -679,7 +679,8 @@ static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr)return changed; } -static u32 deftype_lo, deftype_hi; +static DEFINE_PER_CPU(u32, deftype_lo); +static DEFINE_PER_CPU(u32, deftype_hi);
My APM says that the high 32 bits of the MTRRdefType MSR are reserved and MBZ. So you can drop the _hi thing and use 0 everywhere. Or rather a dummy = 0 var because of that damn rdmsr() macro.
Or simply use a
u64 deftype;
use the rdmsrl/wrmsrl() variants and split it when passing to umtrr_wrmsr() because that thing wants 2 32s.
arch/x86/kernel/cpu/mtrr/generic.c static unsigned long set_mtrr_state(void) · line 695
Quoted 7 lines
arch/x86/kernel/cpu/mtrr/generic.c +1
-0/** * set_mtrr_state - Set the MTRR state for this CPU. @@ -691,6 +692,7 @@ static unsigned long set_mtrr_state(void) {unsigned long change_mask =0 ;unsigned int i; +u32 *lo =this_cpu_ptr (&deftype_lo);
The tip-tree preferred ordering of variable declarations at the beginning of a function is reverse fir tree order::
struct long_struct_name *descriptive_name; unsigned long foo, bar; unsigned int tmp; int ret;
The above is faster to parse than the reverse ordering::
int ret; unsigned int tmp; unsigned long foo, bar; struct long_struct_name *descriptive_name;
And even more so than random ordering::
unsigned long foo, bar; int ret; struct long_struct_name *descriptive_name; unsigned int tmp;
Please check all your patches.
Quoted 17 lines
arch/x86/kernel/cpu/mtrr/generic.c +3
-3for (i =0 ; i < num_var_ranges; i++) {if (set_mtrr_var_ranges (i, &mtrr_state.var_ranges[i]))@@ -704,10 +706,10 @@ static unsigned long set_mtrr_state(void)* Set_mtrr_restore restores the old value of MTRRdefType, * so to set it we fiddle with the saved value: */ -if ((deftype_lo &0xff ) != mtrr_state.def_type - || ((deftype_lo &0xc00 ) >>10 ) != mtrr_state.enabled) { +if ((*lo &0xff ) != mtrr_state.def_type + || ((*lo &0xc00 ) >>10 ) != mtrr_state.enabled) { - deftype_lo = (deftype_lo & ~0xcff ) | mtrr_state.def_type | + *lo = (*lo & ~0xcff ) | mtrr_state.def_type | (mtrr_state.enabled <<10 ); change_mask |=MTRR_CHANGE_MASK_DEFTYPE ; }
arch/x86/kernel/cpu/mtrr/generic.c static DEFINE_RAW_SPINLOCK(set_atomicity_lock); · line 735
Quoted 5 lines
arch/x86/kernel/cpu/mtrr/generic.c +2
-0@@ -729,6 +731,8 @@ static DEFINE_RAW_SPINLOCK(set_atomicity_lock);static void prepare_set (void )__acquires (set_atomicity_lock) {unsigned long cr0; +u32 *lo =this_cpu_ptr (&deftype_lo); +u32 *hi =this_cpu_ptr (&deftype_hi);
You don't need the pointers here - this_cpu_read() should be enough.
Quoted 16 lines
arch/x86/kernel/cpu/mtrr/generic.c +2
-2/* * Note that this is not ideal @@ -763,10 +767,10 @@ static void prepare_set(void) __acquires(set_atomicity_lock)flush_tlb_local ();/* Save MTRR state */ -rdmsr (MSR_MTRRdefType, deftype_lo, deftype_hi); +rdmsr (MSR_MTRRdefType, *lo, *hi);/* Disable MTRRs, and set the default type to uncached */ -mtrr_wrmsr (MSR_MTRRdefType, deftype_lo & ~0xcff , deftype_hi); +mtrr_wrmsr (MSR_MTRRdefType, *lo & ~0xcff , *hi);/* Again, only flush caches if we have to. */ if (!static_cpu_has (X86_FEATURE_SELFSNOOP ))
arch/x86/kernel/cpu/mtrr/generic.c static void prepare_set(void) __acquires(set_atomicity_lock) · line 783
Quoted 5 lines
arch/x86/kernel/cpu/mtrr/generic.c +2
-0@@ -775,12 +779,15 @@ static void prepare_set(void) __acquires(set_atomicity_lock)static void post_set (void )__releases (set_atomicity_lock) { +u32 *lo =this_cpu_ptr (&deftype_lo); +u32 *hi =this_cpu_ptr (&deftype_hi);
Ditto.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH v2 01/10] x86/mtrr: fix MTRR fixup on APs
Quoted 11 lines · Borislav Petkov
Quoted 10 lines
Fix that by using percpu variables for saving the MSR contents.
Signed-off-by Juergen Gross <[email protected]>---
I thought adding a "Fixes:" tag for the kernel's initial git commit would maybe be entertaining, but without being really helpful. The percpu variables were preferred over on-stack ones in order to avoid more code churn in followup patches decoupling PAT from MTRR support.
Quoted 2 lines
So if that thing has been broken for so long and no one noticed, we could just as well not backport to stable at all...
Yeah, you can't do that.
The whole day today I kept thinking that something's wrong with this here. As in, why hasn't it been reported until now.
You say above:
"... for all cpus is racy in case the MSR contents differ across cpus."
But they don't differ:
"7.7.5 MTRRs in Multi-Processing Environments
In multi-processing environments, the MTRRs located in all processors must characterize memory in the same way. Generally, this means that identical values are written to the MTRRs used by the processors. This also means that values CR0.CD and the PAT must be consistent across processors. Failure to do so may result in coherency violations or loss of atomicity. Processor implementations do not check the MTRR settings in other processors to ensure consistency. It is the responsibility of system software to initialize and maintain MTRR consistency across all processors."
And you can't have different fixed MTRR type on each CPU - that would lead to all kinds of nasty bugs.
And here's from a big fat box:
$ rdmsr -a 0x2ff | uniq -c
256 c00All 256 CPUs have the def type set to the same thing.
Now, if all CPUs go write that same deftype_lo variable in the rendezvous handler, the only issue that could happen is if a read sees a partial write. BUT, AFAIK, x86 doesn't tear 32-bit writes so I *think* all CPUs see the same value being corrected by using mtrr_state previously saved on the BSP.
As I said, we should've seen this exploding left and right otherwise...
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v2 01/10] x86/mtrr: fix MTRR fixup on APs
Quoted 48 lines · Borislav Petkov
Quoted 11 lines · Borislav Petkov
Quoted 10 lines
Fix that by using percpu variables for saving the MSR contents.
Signed-off-by Juergen Gross <[email protected]>---
I thought adding a "Fixes:" tag for the kernel's initial git commit would maybe be entertaining, but without being really helpful. The percpu variables were preferred over on-stack ones in order to avoid more code churn in followup patches decoupling PAT from MTRR support.Quoted 2 lines
So if that thing has been broken for so long and no one noticed, we could just as well not backport to stable at all...
Yeah, you can't do that.
The whole day today I kept thinking that something's wrong with this here. As in, why hasn't it been reported until now.
You say above:
"... for all cpus is racy in case the MSR contents differ across cpus."
But they don't differ:
"7.7.5 MTRRs in Multi-Processing Environments
In multi-processing environments, the MTRRs located in all processors must characterize memory in the same way. Generally, this means that identical values are written to the MTRRs used by the processors. This also means that values CR0.CD and the PAT must be consistent across processors. Failure to do so may result in coherency violations or loss of atomicity. Processor implementations do not check the MTRR settings in other processors to ensure consistency. It is the responsibility of system software to initialize and maintain MTRR consistency across all processors."
And you can't have different fixed MTRR type on each CPU - that would lead to all kinds of nasty bugs.
And here's from a big fat box:
$ rdmsr -a 0x2ff | uniq -c 256 c00All 256 CPUs have the def type set to the same thing.
Quoted 7 lines
Now, if all CPUs go write that same
deftype_lovariable in the rendezvous handler, the only issue that could happen is if a read sees a partial write. BUT, AFAIK, x86 doesn't tear 32-bit writes so I *think* all CPUs see the same value being corrected by usingmtrr_statepreviously saved on the BSP.As I said, we should've seen this exploding left and right otherwise...
And then there is mtrr_state_warn() in arch/x86/kernel/cpu/mtrr/generic.c which has a comment saying:
/* Some BIOS's are messed up and don't set all MTRRs the same! */
Yes, the chances are slim to hit such a box, but your reasoning suggests I should remove the related code?
Juergen
Borislav Petkov
maintainerRe: [PATCH v2 01/10] x86/mtrr: fix MTRR fixup on APs
Quoted 4 lines · Juergen Gross
And then there is
mtrr_state_warn()inarch/x86/kernel/cpu/mtrr/generic.cwhich has a comment saying:/* Some BIOS's are messed up and don't set all MTRRs the same! */
That thing also says:
pr_info("mtrr: probably your BIOS does not setup all CPUs.\n");
pr_info("mtrr: corrected configuration.\n");because it'll go and force on all CPUs the MTRR state it read from the BSP in mtrr_bp_init->get_mtrr_state.
Quoted 1 line
Yes, the chances are slim to hit such a box,
Well, my workstation says:
$ dmesg | grep -i mtrr [ 0.391514] mtrr: your CPUs had inconsistent variable MTRR settings [ 0.395199] mtrr: probably your BIOS does not setup all CPUs. [ 0.399199] mtrr: corrected configuration.
but that's the variable MTRRs.
Quoted 1 line
but your reasoning suggests I should remove the related code?
My reasoning says you should not do anything at all here - works as advertized. :-)
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v2 01/10] x86/mtrr: fix MTRR fixup on APs
Quoted 23 lines · Borislav Petkov
Quoted 4 lines · Juergen Gross
And then there is
mtrr_state_warn()inarch/x86/kernel/cpu/mtrr/generic.cwhich has a comment saying:/* Some BIOS's are messed up and don't set all MTRRs the same! */
That thing also says:
pr_info("mtrr: probably your BIOS does not setup all CPUs.\n"); pr_info("mtrr: corrected configuration.\n");because it'll go and force on all CPUs the MTRR state it read from the BSP in
mtrr_bp_init->get_mtrr_state.Quoted 1 line
Yes, the chances are slim to hit such a box,
Well, my workstation says:
$ dmesg | grep -i mtrr [ 0.391514] mtrr: your CPUs had inconsistent variable MTRR settings [ 0.395199] mtrr: probably your BIOS does not setup all CPUs. [ 0.399199] mtrr: corrected configuration.
Quoted 7 lines
but that's the variable MTRRs.
Quoted 1 line
but your reasoning suggests I should remove the related code?
My reasoning says you should not do anything at all here - works as advertized. :-)
And what about the:
pr_warn("mtrr: your CPUs had inconsistent MTRRdefType settings\n");
This is the case the patch would fix.
Juergen
[tip: x86/cpu] x86/mtrr: Add comment for set_mtrr_state() serialization
In reply to Juergen GrossThe following commit has been merged into the x86/cpu branch of tip:
The following commit has been merged into the x86/cpu branch of tip:
Commit-ID: 01c97c7303580682751b5aaae043b639bdcbacb3 Gitweb: https://git.kernel.org/tip/01c97c7303580682751b5aaae043b639bdcbacb3 Author: Juergen Gross <[email protected]> AuthorDate: Tue, 04 Oct 2022 10:10:08 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 19 Oct 2022 20:37:23 +02:00
x86/mtrr: Add comment for set_mtrr_state() serialization
Add a comment about set_mtrr_state() needing serialization.
[ bp: Touchups. ]
--- arch/x86/kernel/cpu/mtrr/generic.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/mtrr/generic.c +4 -1
@@ -684,7 +684,10 @@ static u32 deftype_lo, deftype_hi;/** * set_mtrr_state - Set the MTRR state for this CPU. * -* NOTE: The CPU must already be in a safe state for MTRR changes. +* NOTE: The CPU must already be in a safe state for MTRR changes, including +* measures that only a single CPU can be active in set_mtrr_state() in +* order to not be subject to races for usage of deftype_lo. This is +* accomplished by taking set_atomicity_lock. * RETURNS: 0 if no changes made, else a mask indicating what was changed. */ static unsigned long set_mtrr_state (void )
PATCH v2 2/10 x86/mtrr: remove unused cyrix_set_all() function
In reply to Juergen GrossThe Cyrix cpu specific MTRR function cyrix_set_all() will never be called, as the struct mtrr_ops set_all() callback will only be called in the use_intel() case, which would require the use_intel_if member of struct mtrr_ops to be set, which isn't the case for Cyrix.
---
V2:
- new patch
--- arch/x86/kernel/cpu/mtrr/cyrix.c | 34 -------------------------------- 1 file changed, 34 deletions(-)
arch/x86/kernel/cpu/mtrr/cyrix.c +0 -34
@@ -234,42 +234,8 @@ static void cyrix_set_arr(unsigned int reg, unsigned long base,post_set (); } -typedef struct { -unsigned long base; -unsigned long size; -mtrr_type type; -}arr_state_t ; - -static arr_state_t arr_state[8 ] = { - {0UL ,0UL ,0UL }, {0UL ,0UL ,0UL }, {0UL ,0UL ,0UL }, {0UL ,0UL ,0UL }, - {0UL ,0UL ,0UL }, {0UL ,0UL ,0UL }, {0UL ,0UL ,0UL }, {0UL ,0UL ,0UL } -}; - -static unsigned char ccr_state[7 ] = {0 ,0 ,0 ,0 ,0 ,0 ,0 }; - -static void cyrix_set_all (void ) -{ -int i; - -prepare_set (); - -/* the CCRs are not contiguous */ -for (i =0 ; i <4 ; i++) -setCx86 (CX86_CCR0 + i, ccr_state[i]); -for (; i <7 ; i++) -setCx86 (CX86_CCR4 + i, ccr_state[i]); - -for (i =0 ; i <8 ; i++) { -cyrix_set_arr (i, arr_state[i].base, - arr_state[i].size, arr_state[i].type); - } - -post_set (); -} -static const struct mtrr_ops cyrix_mtrr_ops = { .vendor =X86_VENDOR_CYRIX , - .set_all = cyrix_set_all, .set = cyrix_set_arr, .get = cyrix_get_arr, .get_free_region = cyrix_get_free_region,
Signature · 1 line
2.35.3
Borislav Petkov
maintainerRe: [PATCH v2 02/10] x86/mtrr: remove unused cyrix_set_all() function
Quoted 4 lines · Juergen Gross
The Cyrix cpu specific MTRR function
cyrix_set_all()will never be called, as the structmtrr_opsset_all()callback will only be called in theuse_intel()case, which would require theuse_intel_ifmember of structmtrr_opsto be set, which isn't the case for Cyrix.
Doing some git archeology:
So the commit which added mtrr_aps_delayed_init is
d0af9eed5aa9 ("x86, pat/mtrr: Rendezvous all the cpus for MTRR/PAT init")
from 2009.
The IPI callback before it, looked like this:
static void ipi_handler(void *info)
{
#ifdef CONFIG_SMP
struct set_mtrr_data *data = info;
unsigned long flags;
local_irq_save(flags);
atomic_dec(&data->count);
while (!atomic_read(&data->gate))
cpu_relax();
/* The master has cleared me to execute */
if (data->smp_reg != ~0U) {
mtrr_if->set(data->smp_reg, data->smp_base,
data->smp_size, data->smp_type);
} else {
mtrr_if->set_all();
^^^^^^^^^and that else branch would call ->set_all() on Cyrix too.
Suresh's patch changed it to do:
- } else {
+ } else if (mtrr_aps_delayed_init) {
+ /*
+ * Initialize the MTRRs inaddition to the synchronisation.
+ */
mtrr_if->set_all();
BUT below in the set_mtrr() call, it did:
/*
* HACK!
* We use this same function to initialize the mtrrs on boot.
* The state of the boot cpu's mtrrs has been saved, and we want
* to replicate across all the APs.
* If we're doing that @reg is set to something special... */
if (reg != ~0U)
mtrr_if->set(reg, base, size, type);
else if (!mtrr_aps_delayed_init)
mtrr_if->set_all();
^^^
and that would be the Cyrix case.
But then
192d8857427d ("x86, mtrr: use stop_machine APIs for doing MTRR rendezvous")
came and "cleaned" all up by removing the "HACK" and doing ->set_all() only in the rendezvous handler:
+ } else if (mtrr_aps_delayed_init || !cpu_online(smp_processor_id())) {
mtrr_if->set_all();
}Which begs the question: why doesn't the second part of the else test match on Cyrix? The "|| !cpu_online(smp_processor_id())" case.
If only we had a Cyrix machine somewhere...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v2 02/10] x86/mtrr: remove unused cyrix_set_all() function
Quoted 71 lines · Borislav Petkov
Quoted 4 lines · Juergen Gross
The Cyrix cpu specific MTRR function
cyrix_set_all()will never be called, as the structmtrr_opsset_all()callback will only be called in theuse_intel()case, which would require theuse_intel_ifmember of structmtrr_opsto be set, which isn't the case for Cyrix.Doing some git archeology:
So the commit which added
mtrr_aps_delayed_initis
d0af9eed5aa9("x86, pat/mtrr: Rendezvous all the cpus for MTRR/PAT init")from 2009.
The IPI callback before it, looked like this:
static void
ipi_handler(void *info)
{#ifdef CONFIG_SMP struct set_mtrr_data *data = info; unsigned long flags; local_irq_save(flags); atomic_dec(&data->count); while (!atomic_read(&data->gate)) cpu_relax(); /* The master has cleared me to execute */ if (data->smp_reg != ~0U) { mtrr_if->set(data->smp_reg, data->smp_base, data->smp_size, data->smp_type); } else { mtrr_if->set_all(); ^^^^^^^^^and that else branch would call ->set_all() on Cyrix too.
Suresh's patch changed it to do:
(quoted diff) +4
-1- } else { + } else if (mtrr_aps_delayed_init) { + /* + * Initialize the MTRRs inaddition to the synchronisation. + */ mtrr_if->set_all();BUT below in the
set_mtrr()call, it did:/*
* HACK!
* We use this same function to initialize the mtrrs on boot.
* The state of the boot cpu's mtrrs has been saved, and we want
* to replicate across all the APs.
* If we're doing that @reg is set to something special... */
if (reg != ~0U)
mtrr_if->set(reg, base, size, type);
else if (!mtrr_aps_delayed_init)
mtrr_if->set_all();
^^^and that would be the Cyrix case.
But then
192d8857427d("x86, mtrr: usestop_machineAPIs for doing MTRR rendezvous")came and "cleaned" all up by removing the "HACK" and doing ->set_all() only in the rendezvous handler:
Quoted 9 lines
+ } else if (mtrr_aps_delayed_init || !cpu_online(smp_processor_id())) { mtrr_if->set_all(); }Which begs the question: why doesn't the second part of the else test match on Cyrix? The "|| !cpu_online(smp_processor_id())" case.
If only we had a Cyrix machine somewhere...
You are missing one aspect here: there is no call path for Cyrix CPUs using reg == ~0U.
So the condition of the "else if" will never be evaluated with Cyrix.
Juergen
Re: [PATCH v2 02/10] x86/mtrr: remove unused cyrix_set_all() function
In reply to Borislav PetkovQuoted 71 lines · Borislav Petkov
Quoted 4 lines · Juergen Gross
The Cyrix cpu specific MTRR function
cyrix_set_all()will never be called, as the structmtrr_opsset_all()callback will only be called in theuse_intel()case, which would require theuse_intel_ifmember of structmtrr_opsto be set, which isn't the case for Cyrix.Doing some git archeology:
So the commit which added
mtrr_aps_delayed_initis
d0af9eed5aa9("x86, pat/mtrr: Rendezvous all the cpus for MTRR/PAT init")from 2009.
The IPI callback before it, looked like this:
static void
ipi_handler(void *info)
{#ifdef CONFIG_SMP struct set_mtrr_data *data = info; unsigned long flags; local_irq_save(flags); atomic_dec(&data->count); while (!atomic_read(&data->gate)) cpu_relax(); /* The master has cleared me to execute */ if (data->smp_reg != ~0U) { mtrr_if->set(data->smp_reg, data->smp_base, data->smp_size, data->smp_type); } else { mtrr_if->set_all(); ^^^^^^^^^and that else branch would call ->set_all() on Cyrix too.
Suresh's patch changed it to do:
(quoted diff) +4
-1- } else { + } else if (mtrr_aps_delayed_init) { + /* + * Initialize the MTRRs inaddition to the synchronisation. + */ mtrr_if->set_all();BUT below in the
set_mtrr()call, it did:/*
* HACK!
* We use this same function to initialize the mtrrs on boot.
* The state of the boot cpu's mtrrs has been saved, and we want
* to replicate across all the APs.
* If we're doing that @reg is set to something special... */
if (reg != ~0U)
mtrr_if->set(reg, base, size, type);
else if (!mtrr_aps_delayed_init)
mtrr_if->set_all();
^^^and that would be the Cyrix case.
But then
192d8857427d("x86, mtrr: usestop_machineAPIs for doing MTRR rendezvous")came and "cleaned" all up by removing the "HACK" and doing ->set_all() only in the rendezvous handler:
Quoted 9 lines
+ } else if (mtrr_aps_delayed_init || !cpu_online(smp_processor_id())) { mtrr_if->set_all(); }Which begs the question: why doesn't the second part of the else test match on Cyrix? The "|| !cpu_online(smp_processor_id())" case.
If only we had a Cyrix machine somewhere...
Maybe the alternative reasoning is much faster to understand: if the Cyrix set_all() could be called, the AMD and Centaur ones would be callable, too. Those being called would result in a NULL deref, so why should we keep the Cyrix one?
Juergen
Borislav Petkov
maintainerRe: [PATCH v2 02/10] x86/mtrr: remove unused cyrix_set_all() function
Quoted 3 lines · Juergen Gross
Maybe the alternative reasoning is much faster to understand: if the Cyrix
set_all()could be called, the AMD and Centaur ones would be callable, too.
Right.
Quoted 2 lines
Those being called would result in a NULL deref, so why should we keep the Cyrix one?
I know you're eager to remove dead code - I'd love that too. But before we do that, we need to find out whether some Cyrix hw out there would not need this.
I know, I know, they should've complained by now ... maybe they have but we haven't heard about it.
What it most likely looks like is that those machines - a commit from before git
commit 8fbdcb188e31ac901e216b466b97e90e8b057daa
Author: Dave Jones <[email protected]> Date: Wed Aug 14 21:14:22 2002 -0700
[PATCH] Modular x86 MTRR driver.
talks about
+/*
+ * On Cyrix 6x86(MX) and M II the ARR3 is special: it has connection
+ * with the SMM (System Management Mode) mode. So we need the following:
+ * Check whether SMI_LOCK (CCR3 bit 0) is set
+ * if it is set, write a warning message: ARR3 cannot be changed!
+ * (it cannot be changed until the next processor reset)
which sounds like old rust. And which no one uses or such machines are long dead already.
Wikipedia says:
https://en.wikipedia.org/wiki/Cyrix_6x86
"The Cyrix 6x86 is a line of sixth-generation, 32-bit x86 microprocessors designed and released by Cyrix in 1995..."
So I'm thinking removing it would be ok...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v2 02/10] x86/mtrr: remove unused cyrix_set_all() function
Quoted 5 lines · Borislav Petkov
Quoted 3 lines · Juergen Gross
Maybe the alternative reasoning is much faster to understand: if the Cyrix
set_all()could be called, the AMD and Centaur ones would be callable, too.
Quoted 8 lines
Right.
Quoted 2 lines
Those being called would result in a NULL deref, so why should we keep the Cyrix one?
I know you're eager to remove dead code - I'd love that too. But before we do that, we need to find out whether some Cyrix hw out there would not need this.
Back to reasoning #1. Only the use_intel() case calls the code in question with reg == ~0. And use_intel() is clearly not Cyrix.
Juergen
PATCH v2 3/10 x86/mtrr: replace use_intel() with a local flag
In reply to Juergen GrossIn MTRR code use_intel() is only used in one source file, and the relevant use_intel_if member of struct mtrr_ops is set only in generic_mtrr_ops.
Replace use_intel() with a single flag in cacheinfo.c, which can be set when assigning generic_mtrr_ops to mtrr_if. This allows to drop use_intel_if from mtrr_ops, while preparing to support PAT without MTRR. As another preparation for the PAT/MTRR decoupling use a bit for MTRR control and one for PAT control. For now set both bits together, this can be changed later.
As the new flag will be set only if mtrr_enabled is set, the test for mtrr_enabled can be dropped at some places.
At the same time drop the local mtrr_enabled() function and rename the __mtrr_enabled flag to mtrr_enabled.
---
V2:
- new patch
--- arch/x86/include/asm/cacheinfo.h | 5 +++ arch/x86/kernel/cpu/cacheinfo.c | 3 ++ arch/x86/kernel/cpu/mtrr/generic.c | 1 - arch/x86/kernel/cpu/mtrr/mtrr.c | 58 ++++++++++++++---------------- arch/x86/kernel/cpu/mtrr/mtrr.h | 2 -- 5 files changed, 35 insertions(+), 34 deletions(-)
arch/x86/include/asm/cacheinfo.h +5 -0
@@ -2,6 +2,11 @@#ifndef _ASM_X86_CACHEINFO_H#define _ASM_X86_CACHEINFO_H +/* Kernel controls MTRR and/or PAT MSRs. */ +extern unsigned int cache_generic; +#define CACHE_GENERIC_MTRR 0x01 +#define CACHE_GENERIC_PAT 0x02 +void cacheinfo_amd_init_llc_id (struct cpuinfo_x86 *c,int cpu);void cacheinfo_hygon_init_llc_id (struct cpuinfo_x86 *c,int cpu);
arch/x86/kernel/cpu/cacheinfo.c +3 -0
@@ -35,6 +35,9 @@ DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);/* Shared L2 cache maps */ DEFINE_PER_CPU_READ_MOSTLY (cpumask_var_t, cpu_l2c_shared_map); +/* Kernel controls MTRR and/or PAT MSRs. */ +unsigned int cache_generic; +struct _cache_table {unsigned char descriptor;char cache_type;
arch/x86/kernel/cpu/mtrr/generic.c +0 -1
@@ -921,7 +921,6 @@ int positive_have_wrcomb(void)* Generic structure... */ const struct mtrr_ops generic_mtrr_ops = { - .use_intel_if =1 , .set_all = generic_set_all, .get = generic_get_mtrr, .get_free_region = generic_get_free_region,
arch/x86/kernel/cpu/mtrr/mtrr.c +27 -31
175 lines not on this page · raw message
arch/x86/kernel/cpu/mtrr/mtrr.h +0 -2
@@ -14,7 +14,6 @@ extern unsigned int mtrr_usage_table[MTRR_MAX_VAR_RANGES];struct mtrr_ops {u32 vendor; -u32 use_intel_if;void (*set)(unsigned int reg,unsigned long base,unsigned long size,mtrr_type type);void (*set_all)(void );
@@ -61,7 +60,6 @@ extern u64 size_or_mask, size_and_mask;extern const struct mtrr_ops *mtrr_if;#define is_cpu (vnd) (mtrr_if && mtrr_if->vendor == X86_VENDOR_##vnd) -#define use_intel () (mtrr_if && mtrr_if->use_intel_if == 1)extern unsigned int num_var_ranges;extern u64 mtrr_tom2;
Signature · 1 line
2.35.3
PATCH v2 4/10 x86: move some code out of arch/x86/kernel/cpu/mtrr
In reply to Juergen GrossPrepare making PAT and MTRR support independent from each other by moving some code needed by both out of the MTRR specific sources.
---
V2:
- move code from cpu/common.c to cpu/cacheinfo.c (Boris Petkov)
--- arch/x86/include/asm/cacheinfo.h | 3 ++ arch/x86/include/asm/mtrr.h | 4 ++ arch/x86/kernel/cpu/cacheinfo.c | 77 ++++++++++++++++++++++++++++ arch/x86/kernel/cpu/mtrr/generic.c | 80 ++++-------------------------- 4 files changed, 93 insertions(+), 71 deletions(-)
arch/x86/include/asm/cacheinfo.h +3 -0
@@ -10,4 +10,7 @@ extern unsigned int cache_generic;void cacheinfo_amd_init_llc_id (struct cpuinfo_x86 *c,int cpu);void cacheinfo_hygon_init_llc_id (struct cpuinfo_x86 *c,int cpu); +void cache_disable (void ); +void cache_enable (void ); +#endif /* _ASM_X86_CACHEINFO_H */
arch/x86/include/asm/mtrr.h +4 -0
@@ -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
arch/x86/kernel/cpu/cacheinfo.c +77 -0
86 lines not on this page · raw message
arch/x86/kernel/cpu/mtrr/generic.c +9 -71
148 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH v2 5/10 x86/mtrr: split generic_set_all()
In reply to Juergen GrossSplit generic_set_all() into multiple parts, while moving the main function body into cacheinfo.c.
This prepares the support of PAT without needing MTRR support by moving the main function body of generic_set_all() into cacheinfo.c while renaming it to cache_cpu_init(). The MTRR specific parts are moved into a dedicated small function called by cache_cpu_init(). The PAT and MTRR specific functions are called conditionally based on the cache_generic bit settings.
The setting of smp_changes_mask is merged into the (new) function mtrr_generic_set_state() used to call set_mtrr_state(). It was probably split in ancient times, as atomic operations while running uncached might be quite expensive, but OTOH only systems with a broken BIOS should ever require to set any bit in smp_changes_mask, so just hurting those devices with a penalty of a few microseconds during boot shouldn't be a real issue.
---
V2:
- new patch
--- arch/x86/include/asm/cacheinfo.h | 1 + arch/x86/include/asm/mtrr.h | 2 ++ arch/x86/kernel/cpu/cacheinfo.c | 19 +++++++++++++++++++ arch/x86/kernel/cpu/mtrr/generic.c | 15 ++------------- 4 files changed, 24 insertions(+), 13 deletions(-)
arch/x86/include/asm/cacheinfo.h +1 -0
@@ -12,5 +12,6 @@ void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c, int cpu);void cache_disable (void );void cache_enable (void ); +void cache_cpu_init (void );#endif /* _ASM_X86_CACHEINFO_H */
arch/x86/include/asm/mtrr.h +2 -0
@@ -50,6 +50,7 @@ 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 ); +void mtrr_generic_set_state (void );# else static inline u8 mtrr_type_lookup (u64 addr,u64 end,u8 *uniform) {
@@ -91,6 +92,7 @@ static inline void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi)#define mtrr_bp_restore () do {} while (0)#define mtrr_disable () do {} while (0)#define mtrr_enable () do {} while (0) +#define mtrr_generic_set_state () do {} while (0)# endif #ifdef CONFIG_COMPAT
arch/x86/kernel/cpu/cacheinfo.c +19 -0
@@ -1120,3 +1120,22 @@ void cache_enable(void) __releases(cache_disable_lock)raw_spin_unlock (&cache_disable_lock); } + +void cache_cpu_init (void ) +{ +unsigned long flags; + +local_irq_save (flags); +cache_disable (); + +/* Set MTRR state. */ +if (cache_generic &CACHE_GENERIC_MTRR ) +mtrr_generic_set_state (); + +/* Set PAT. */ +if (cache_generic &CACHE_GENERIC_PAT ) +pat_init (); + +cache_enable (); +local_irq_restore (flags); +}
arch/x86/kernel/cpu/mtrr/generic.c +2 -13
@@ -736,30 +736,19 @@ void mtrr_enable(void)mtrr_wrmsr (MSR_MTRRdefType, *lo, *hi); } -static void generic_set_all (void ) +void mtrr_generic_set_state (void ) {unsigned long mask, count; -unsigned long flags; - -local_irq_save (flags); -cache_disable ();/* Actually set the state */ mask =set_mtrr_state (); -/* also set PAT */ -pat_init (); - -cache_enable (); -local_irq_restore (flags); -/* Use the atomic bitops to update the global mask */ for (count =0 ; count <sizeof (mask) *8 ; ++count) {if (mask &0x01 )set_bit (count, &smp_changes_mask); mask >>=1 ; } - }/**
@@ -859,7 +848,7 @@ int positive_have_wrcomb(void)* Generic structure... */ const struct mtrr_ops generic_mtrr_ops = { - .set_all = generic_set_all, + .set_all = cache_cpu_init, .get = generic_get_mtrr, .get_free_region = generic_get_free_region, .set = generic_set_mtrr,
Signature · 1 line
2.35.3
PATCH v2 6/10 x86/mtrr: remove set_all callback from struct mtrr_ops
In reply to Juergen GrossInstead of using an indirect call to mtrr_if->set_all just call the only possible target cache_cpu_init() directly. This enables to remove the set_all callback from struct mtrr_ops.
--- arch/x86/kernel/cpu/mtrr/generic.c | 1 - arch/x86/kernel/cpu/mtrr/mtrr.c | 10 +++++----- arch/x86/kernel/cpu/mtrr/mtrr.h | 2 -- 3 files changed, 5 insertions(+), 8 deletions(-)
arch/x86/kernel/cpu/mtrr/generic.c +0 -1
@@ -848,7 +848,6 @@ int positive_have_wrcomb(void)* Generic structure... */ const struct mtrr_ops generic_mtrr_ops = { - .set_all = cache_cpu_init, .get = generic_get_mtrr, .get_free_region = generic_get_free_region, .set = generic_set_mtrr,
arch/x86/kernel/cpu/mtrr/mtrr.c +5 -5
@@ -165,15 +165,15 @@ static int mtrr_rendezvous_handler(void *info)* saved, and we want to replicate that across all the cpus that come * online (either at the end of boot or resume or during a runtime cpu * online). If we're doing that, @reg is set to something special and on -* all the cpu's we do mtrr_if->set_all() (On the logical cpu that +* all the cpu's we do cache_cpu_init() (On the logical cpu that * started the boot/resume sequence, this might be a duplicate -* set_all()). +* cache_cpu_init()). */ if (data->smp_reg != ~0U ) { mtrr_if->set (data->smp_reg, data->smp_base, data->smp_size, data->smp_type); }else if (mtrr_aps_delayed_init || !cpu_online (smp_processor_id ())) { - mtrr_if->set_all (); +cache_cpu_init (); }return 0 ; }
@@ -768,7 +768,7 @@ void __init mtrr_bp_init(void)if (mtrr_cleanup (phys_addr)) { changed_by_mtrr_cleanup =1 ; - mtrr_if->set_all (); +cache_cpu_init (); } } }
@@ -854,7 +854,7 @@ void mtrr_bp_restore(void)if (!cache_generic)return ; - mtrr_if->set_all (); +cache_cpu_init (); }static int __init mtrr_init_finialize (void )
arch/x86/kernel/cpu/mtrr/mtrr.h +0 -2
@@ -16,8 +16,6 @@ struct mtrr_ops {u32 vendor;void (*set)(unsigned int reg,unsigned long base,unsigned long size,mtrr_type type); -void (*set_all)(void ); -void (*get)(unsigned int reg,unsigned long *base,unsigned long *size,mtrr_type *type); int (*get_free_region)(unsigned long base, unsigned long size,
Signature · 1 line
2.35.3
PATCH v2 7/10 x86/mtrr: simplify mtrr_bp_init()
In reply to Juergen GrossIn case of the generic cache interface being used (Intel cpus or a 64-bit system), the initialization sequence of the boot cpu is more complicated as necessary:
- check if MTRR enabled, if yes, call mtrr_bp_pat_init() which will disable caching, set the PAT MSR, and reenable caching
- call mtrr_cleanup(), in case that changed anything, call cache_cpu_init() doing the same caching disable/enable dance as above, but this time with setting the (modified) MTRR state (even if MTRR was disabled) AND setting the PAT MSR (again even with disabled MTRR)
The sequence can be simplified a lot while removing potential inconsistencies:
- check if MTRR enabled, if yes, call mtrr_cleanup() and then cache_cpu_init()
This ensures to:
- no longer disable/enable caching more than once
- avoid to set MTRRs and/or the PAT MSR on the boot processor in case of MTRR cleanups even if MTRRs meant to be disabled
With that mtrr_bp_pat_init() can be removed.
---
V2:
- new patch
--- arch/x86/kernel/cpu/mtrr/generic.c | 14 -------------- arch/x86/kernel/cpu/mtrr/mtrr.c | 7 ++----- arch/x86/kernel/cpu/mtrr/mtrr.h | 1 - 3 files changed, 2 insertions(+), 20 deletions(-)
arch/x86/kernel/cpu/mtrr/generic.c +0 -14
@@ -442,20 +442,6 @@ static void __init print_mtrr_state(void)pr_debug ("TOM2: %016llx aka %lldM\n" , mtrr_tom2, mtrr_tom2>>20 ); } -/* PAT setup for BP. We need to go through sync steps here */ -void __init mtrr_bp_pat_init (void ) -{ -unsigned long flags; - -local_irq_save (flags); -cache_disable (); - -pat_init (); - -cache_enable (); -local_irq_restore (flags); -} -/* Grab all of the MTRR state for this CPU into *state */ bool __init get_mtrr_state (void ) {
arch/x86/kernel/cpu/mtrr/mtrr.c +2 -5
@@ -761,13 +761,10 @@ void __init mtrr_bp_init(void) mtrr_enabled =get_mtrr_state ();if (mtrr_enabled) { -mtrr_bp_pat_init (); cache_generic |=CACHE_GENERIC_MTRR |CACHE_GENERIC_PAT ; - } - -if (mtrr_cleanup (phys_addr)) { - changed_by_mtrr_cleanup =1 ; + changed_by_mtrr_cleanup = +mtrr_cleanup (phys_addr);cache_cpu_init (); } }
arch/x86/kernel/cpu/mtrr/mtrr.h +0 -1
@@ -50,7 +50,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 mtrr_bp_pat_init (void );extern void __init set_mtrr_ops (const struct mtrr_ops *ops);
Signature · 1 line
2.35.3
PATCH v2 8/10 x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init
In reply to Juergen GrossIn order to prepare decoupling MTRR and PAT replace the MTRR specific mtrr_aps_delayed_init flag with a more generic cache_aps_delayed_init one.
---
V2:
- new patch
--- arch/x86/include/asm/cacheinfo.h | 2 ++ arch/x86/include/asm/mtrr.h | 2 -- arch/x86/kernel/cpu/cacheinfo.c | 2 ++ arch/x86/kernel/cpu/mtrr/mtrr.c | 17 ++++------------- arch/x86/kernel/smpboot.c | 5 +++-- 5 files changed, 11 insertions(+), 17 deletions(-)
arch/x86/include/asm/cacheinfo.h +2 -0
@@ -7,6 +7,8 @@ extern unsigned int cache_generic;#define CACHE_GENERIC_MTRR 0x01#define CACHE_GENERIC_PAT 0x02 +extern bool cache_aps_delayed_init; +void cacheinfo_amd_init_llc_id (struct cpuinfo_x86 *c,int cpu);void cacheinfo_hygon_init_llc_id (struct cpuinfo_x86 *c,int cpu);
arch/x86/include/asm/mtrr.h +0 -2
@@ -43,7 +43,6 @@ 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 set_mtrr_aps_delayed_init (void );extern void mtrr_aps_init (void );extern void mtrr_bp_restore (void );extern int mtrr_trim_uncached_memory (unsigned long end_pfn);
@@ -87,7 +86,6 @@ static inline void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi) { }#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)#define mtrr_disable () do {} while (0)
arch/x86/kernel/cpu/cacheinfo.c +2 -0
@@ -1139,3 +1139,5 @@ void cache_cpu_init(void)cache_enable ();local_irq_restore (flags); } + +bool cache_aps_delayed_init;
arch/x86/kernel/cpu/mtrr/mtrr.c +4 -13
@@ -65,7 +65,6 @@ unsigned int mtrr_usage_table[MTRR_MAX_VAR_RANGES];static DEFINE_MUTEX (mtrr_mutex);u64 size_or_mask, size_and_mask; -static bool mtrr_aps_delayed_init;static const struct mtrr_ops *mtrr_ops[X86_VENDOR_NUM ]__ro_after_init ;
@@ -172,7 +171,7 @@ static int mtrr_rendezvous_handler(void *info)if (data->smp_reg != ~0U ) { mtrr_if->set (data->smp_reg, data->smp_base, data->smp_size, data->smp_type); - }else if (mtrr_aps_delayed_init || !cpu_online (smp_processor_id ())) { + }else if (cache_aps_delayed_init || !cpu_online (smp_processor_id ())) {cache_cpu_init (); }return 0 ;
@@ -784,7 +783,7 @@ void __init mtrr_bp_init(void)void mtrr_ap_init (void ) { -if (!cache_generic || mtrr_aps_delayed_init) +if (!cache_generic || cache_aps_delayed_init)return ;/*
@@ -818,14 +817,6 @@ void mtrr_save_state(void)smp_call_function_single (first_cpu, mtrr_save_fixed_ranges,NULL ,1 ); } -void set_mtrr_aps_delayed_init (void ) -{ -if (!cache_generic) -return ; - - mtrr_aps_delayed_init = true; -} -/* * Delayed MTRR initialization for all AP's */
@@ -839,11 +830,11 @@ void mtrr_aps_init(void)* by doing set_mtrr_aps_delayed_init(), prior to this point. If not, * then we are done. */ -if (!mtrr_aps_delayed_init) +if (!cache_aps_delayed_init)return ;set_mtrr (~0U ,0 ,0 ,0 ); - mtrr_aps_delayed_init = false; + cache_aps_delayed_init = false; }void mtrr_bp_restore (void )
arch/x86/kernel/smpboot.c +3 -2
@@ -58,6 +58,7 @@#include <linux/overflow.h> #include <asm/acpi.h> +#include <asm/cacheinfo.h> #include <asm/desc.h> #include <asm/nmi.h> #include <asm/irq.h>
@@ -1428,7 +1429,7 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)uv_system_init (); -set_mtrr_aps_delayed_init (); + cache_aps_delayed_init = true;smp_quirk_init_udelay ();
@@ -1439,7 +1440,7 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)void arch_thaw_secondary_cpus_begin (void ) { -set_mtrr_aps_delayed_init (); + cache_aps_delayed_init = true; }void arch_thaw_secondary_cpus_end (void )
Signature · 1 line
2.35.3
PATCH v2 9/10 x86/mtrr: add a stop_machine() handler calling only cache_cpu_init()
In reply to Juergen GrossInstead of having a stop_machine() handler for either a specific MTRR register or all state at once, add a handler just for calling cache_cpu_init() if appropriate.
Add functions for calling stop_machine() with this handler as well.
Add a generic replacements for mtrr_bp_restore() and a wrapper for mtrr_bp_init().
---
V2:
- completely new replacement of former patch 2
--- arch/x86/include/asm/cacheinfo.h | 5 +- arch/x86/include/asm/mtrr.h | 4 -- arch/x86/kernel/cpu/cacheinfo.c | 59 +++++++++++++++++++++- arch/x86/kernel/cpu/common.c | 3 +- arch/x86/kernel/cpu/mtrr/mtrr.c | 87 +------------------------------- arch/x86/kernel/setup.c | 3 +- arch/x86/kernel/smpboot.c | 4 +- arch/x86/power/cpu.c | 3 +- 8 files changed, 72 insertions(+), 96 deletions(-)
arch/x86/include/asm/cacheinfo.h +4 -1
@@ -14,6 +14,9 @@ void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c, int cpu);void cache_disable (void );void cache_enable (void ); -void cache_cpu_init (void ); +void cache_bp_init (void ); +void cache_bp_restore (void ); +void cache_ap_init (void ); +void cache_aps_init (void );#endif /* _ASM_X86_CACHEINFO_H */
arch/x86/include/asm/mtrr.h +0 -4
@@ -42,8 +42,6 @@ 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_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 );
@@ -85,8 +83,6 @@ static inline int mtrr_trim_uncached_memory(unsigned long end_pfn)static inline void mtrr_centaur_report_mcr (int mcr,u32 lo,u32 hi) { } -#define mtrr_ap_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)
arch/x86/kernel/cpu/cacheinfo.c +58 -1
74 lines not on this page · raw message
arch/x86/kernel/cpu/common.c +2 -1
@@ -52,6 +52,7 @@#include <asm/cpu.h> #include <asm/mce.h> #include <asm/msr.h> +#include <asm/cacheinfo.h> #include <asm/memtype.h> #include <asm/microcode.h> #include <asm/microcode_intel.h>
@@ -1936,7 +1937,7 @@ void identify_secondary_cpu(struct cpuinfo_x86 *c)#ifdef CONFIG_X86_32 enable_sep_cpu ();#endif -mtrr_ap_init (); +cache_ap_init ();validate_apic_and_package_id (c);x86_spec_ctrl_setup_ap ();update_srbds_msr ();
arch/x86/kernel/cpu/mtrr/mtrr.c +2 -85
123 lines not on this page · raw message
arch/x86/kernel/setup.c +2 -1
@@ -34,6 +34,7 @@#include <asm/numa.h> #include <asm/bios_ebda.h> #include <asm/bugs.h> +#include <asm/cacheinfo.h> #include <asm/cpu.h> #include <asm/efi.h> #include <asm/gart.h>
@@ -1075,7 +1076,7 @@ void __init setup_arch(char **cmdline_p)/* update e820 for memory not covered by WB MTRRs */ if (IS_ENABLED (CONFIG_MTRR )) -mtrr_bp_init (); +cache_bp_init ();else pat_disable ("PAT support disabled because CONFIG_MTRR is disabled in the kernel." );
arch/x86/kernel/smpboot.c +2 -2
@@ -1445,7 +1445,7 @@ void arch_thaw_secondary_cpus_begin(void)void arch_thaw_secondary_cpus_end (void ) { -mtrr_aps_init (); +cache_aps_init (); }/*
@@ -1488,7 +1488,7 @@ void __init native_smp_cpus_done(unsigned int max_cpus)nmi_selftest ();impress_friends (); -mtrr_aps_init (); +cache_aps_init (); }static int __initdata setup_possible_cpus =-1 ;
arch/x86/power/cpu.c +2 -1
@@ -23,6 +23,7 @@#include <asm/fpu/api.h> #include <asm/debugreg.h> #include <asm/cpu.h> +#include <asm/cacheinfo.h> #include <asm/mmu_context.h> #include <asm/cpu_device_id.h> #include <asm/microcode.h>
@@ -261,7 +262,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 (); +cache_bp_restore ();perf_restore_debug_store (); c = &cpu_data (smp_processor_id ());
Signature · 1 line
2.35.3
PATCH v2 10/10 x86: decouple pat and mtrr handling
In reply to Juergen GrossToday PAT is usable only with MTRR being active, with some nasty tweaks to make PAT usable when running as Xen PV guest, which doesn't support MTRR.
The reason for this coupling is, that both, PAT MSR changes and MTRR changes, require a similar sequence and so full PAT support was added using the already available MTRR handling.
Xen PV PAT handling can work without MTRR, as it just needs to consume the PAT MSR setting done by the hypervisor without the ability and need to change it. This in turn has resulted in a convoluted initialization sequence and wrong decisions regarding cache mode availability due to misguiding PAT availability flags.
Fix all of that by allowing to use PAT without MTRR and by reworking the current PAT initialization sequence to match better with the newly introduced generic cache initialization.
This removes the need of the recently added pat_force_disabled flag, so remove the remnants of the patch adding it.
---
V2:
- former patch 3 completely reworked
--- arch/x86/include/asm/memtype.h | 5 +- arch/x86/kernel/cpu/cacheinfo.c | 3 +- arch/x86/kernel/cpu/mtrr/mtrr.c | 13 +--- arch/x86/kernel/setup.c | 13 +--- arch/x86/mm/pat/memtype.c | 127 ++++++++++---------------------- 5 files changed, 45 insertions(+), 116 deletions(-)
arch/x86/include/asm/memtype.h +2 -3
@@ -6,9 +6,8 @@#include <asm/pgtable_types.h> extern bool pat_enabled (void ); -extern void pat_disable (const char *reason); -extern void pat_init (void ); -extern void init_cache_modes (void ); +extern void pat_bp_init (void ); +extern void pat_cpu_init (void );extern int memtype_reserve (u64 start,u64 end,enum page_cache_mode req_pcm,enum page_cache_mode *ret_pcm);
arch/x86/kernel/cpu/cacheinfo.c +2 -1
@@ -1135,7 +1135,7 @@ static void cache_cpu_init(void)/* Set PAT. */ if (cache_generic &CACHE_GENERIC_PAT ) -pat_init (); +pat_cpu_init ();cache_enable ();local_irq_restore (flags);
@@ -1154,6 +1154,7 @@ static int cache_rendezvous_handler(void *unused)void __init cache_bp_init (void ) {mtrr_bp_init (); +pat_bp_init ();if (cache_generic)cache_cpu_init ();
arch/x86/kernel/cpu/mtrr/mtrr.c +2 -11
@@ -727,24 +727,15 @@ void __init mtrr_bp_init(void) mtrr_enabled =get_mtrr_state ();if (mtrr_enabled) { - cache_generic |=CACHE_GENERIC_MTRR | -CACHE_GENERIC_PAT ; + cache_generic |=CACHE_GENERIC_MTRR ; changed_by_mtrr_cleanup =mtrr_cleanup (phys_addr); } } } -if (!mtrr_enabled) { +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." ); - } }/**
arch/x86/kernel/setup.c +1 -12
@@ -1075,23 +1075,12 @@ void __init setup_arch(char **cmdline_p) max_pfn =e820__end_of_ram_pfn ();/* update e820 for memory not covered by WB MTRRs */ -if (IS_ENABLED (CONFIG_MTRR )) -cache_bp_init (); -else -pat_disable ("PAT support disabled because CONFIG_MTRR is disabled in the kernel." ); - +cache_bp_init ();if (mtrr_trim_uncached_memory (max_pfn)) max_pfn =e820__end_of_ram_pfn (); max_possible_pfn = max_pfn; -/* -* 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 -* effect. -*/ -init_cache_modes (); -/* * Define random base addresses for memory sections after max_pfn is * defined and before each memory section base is used.
arch/x86/mm/pat/memtype.c +38 -89
220 lines not on this page · raw message
Signature · 1 line
2.35.3