Skip to contentSkip to conversation

x86: make pat and mtrr independent from each other

PATCH Superseded Juergen Gross 2 participants 41 messages Sep 8, 2022 – Sep 30, 2022

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 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 V3:
- replace patch 1 by just adding a comment

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: add comment for set_mtrr_state() serialization 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 | 107 ++-----------------
 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, 265 insertions(+), 385 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH v3 1/10 x86/mtrr: add comment for set_mtrr_state() serialization

Add a comment how set_mtrr_state() is needing serialization.

---
V3:
- new patch instead of old patch 1

---
 arch/x86/kernel/cpu/mtrr/generic.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/mtrr/generic.c +4 -1
index 558108296f3c..cd64eab02393 100644
@@ -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)
Signature · 1 line
2.35.3
#2 lore raw

PATCH v3 2/10 x86/mtrr: remove unused cyrix_set_all() function

In reply to Juergen Gross

The 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
index ca670919b561..c77d3b0a5bf2 100644
@@ -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
#3 lore raw

PATCH v3 3/10 x86/mtrr: replace use_intel() with a local flag

In reply to Juergen Gross

In 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
index 86b2e0dcc4bf..1aeafa9888f7 100644
@@ -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
index 66556833d7af..3b05d3ade7a6 100644
@@ -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
index cd64eab02393..81742870ecc5 100644
@@ -917,7 +917,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
index 2ac99e561181..88b1c4b6174a 100644
@@ -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
maintainer
#4 lore raw

Re: [PATCH v3 03/10] x86/mtrr: replace use_intel() with a local flag

arch/x86/include/asm/cacheinfo.h line 6

Quoted 10 lines · Juergen Gross
arch/x86/include/asm/cacheinfo.h +2 -0
index 86b2e0dcc4bf..1aeafa9888f7 100644
@@ -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;

So this should be called something more descriptive like

memory_caching_types

or so to denote that this is a bitfield of supported memory caching technologies. The code then would read as

if (memory_caching_types & CACHE_MTRR)

The name's still not optimal tho - needs more brooding over.

arch/x86/include/asm/cacheinfo.h line 8

Quoted 2 lines
arch/x86/include/asm/cacheinfo.h +2 -0
+#define CACHE_GENERIC_MTRR 0x01
+#define CACHE_GENERIC_PAT  0x02

And those should be CACHE_{MTRR,PAT}.

arch/x86/kernel/cpu/cacheinfo.c DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map); · line 39

Quoted 13 lines
arch/x86/include/asm/cacheinfo.h +0 -0
 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 +2 -0
index 66556833d7af..3b05d3ade7a6 100644
@@ -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;

This should either be __ro_after_init and initialized to 0 or you need accessors...

arch/x86/kernel/cpu/cacheinfo.c DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);

Quoted 8 lines
arch/x86/kernel/cpu/cacheinfo.c +1 -6
 u32 num_var_ranges;
-static bool __mtrr_enabled;
-
-static bool mtrr_enabled(void)
-{
-	return __mtrr_enabled;
-}
+static bool mtrr_enabled;

Hmm, I don't like this. There's way too many boolean flags in the mtrr code. There's mtrr_state.enabled too. ;-\

Can we set (or clear) X86_FEATURE_MTRR to denote MTRR enablement status and get rid of one more boolean flag?

...

Quoted 15 lines

void __init mtrr_bp_init(void)
{

arch/x86/kernel/cpu/cacheinfo.c +2 -0
+	bool use_generic = false;
 	u32 phys_addr;
 
 	init_ifs();
@@ -694,6 +691,7 @@ void __init mtrr_bp_init(void)
 
 	if (boot_cpu_has(X86_FEATURE_MTRR)) {
 		mtrr_if = &generic_mtrr_ops;
+		use_generic = true;
 		size_or_mask = SIZE_OR_MASK_BITS(36);
 		size_and_mask = 0x00f00000;
 		phys_addr = 36;

arch/x86/kernel/cpu/cacheinfo.c void __init mtrr_bp_init(void) · line 757

Quoted 7 lines
arch/x86/kernel/cpu/cacheinfo.c +2 -2
@@ -755,15 +753,18 @@ void __init mtrr_bp_init(void)
 	}
 
 	if (mtrr_if) {
-		__mtrr_enabled = true;
-		set_num_var_ranges();
+		mtrr_enabled = true;
+		set_num_var_ranges(use_generic);

You don't need use_generic either:

		set_num_var_ranges(mtrr_if == generic_mtrr_ops);

(The reason being I wanna get rid of that nasty minefield of boolean vars all round that code).

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#5 lore raw

Re: [PATCH v3 03/10] x86/mtrr: replace use_intel() with a local flag

Quoted 9 lines · Borislav Petkov
Quoted 8 lines · Juergen Gross
arch/x86/include/asm/cacheinfo.h +0 -0
index 86b2e0dcc4bf..1aeafa9888f7 100644
@@ -2,6 +2,11 @@
  #ifndef _ASM_X86_CACHEINFO_H
  #define _ASM_X86_CACHEINFO_H
 
Quoted 6 lines

arch/x86/include/asm/cacheinfo.h line 6

Quoted 2 lines
arch/x86/include/asm/cacheinfo.h +2 -0
+/* Kernel controls MTRR and/or PAT MSRs. */
+extern unsigned int cache_generic;

So this should be called something more descriptive like

memory_caching_types

In the end this variable doesn't specify which caching types are available, but the ways to select/control the caching types.

So what about "memory_caching_select" or "memory_caching_control" instead?

Quoted 11 lines

or so to denote that this is a bitfield of supported memory caching technologies. The code then would read as

if (memory_caching_types & CACHE_MTRR)

The name's still not optimal tho - needs more brooding over.

arch/x86/include/asm/cacheinfo.h line 8

Quoted 2 lines
arch/x86/include/asm/cacheinfo.h +2 -0
+#define CACHE_GENERIC_MTRR 0x01
+#define CACHE_GENERIC_PAT  0x02

And those should be CACHE_{MTRR,PAT}.

Fine with me.

Quoted 11 lines
Quoted 11 lines
arch/x86/include/asm/cacheinfo.h +0 -0
  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 +0 -0
index 66556833d7af..3b05d3ade7a6 100644
@@ -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);
 
Quoted 5 lines

arch/x86/kernel/cpu/cacheinfo.c DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map); · line 39

Quoted 2 lines
arch/x86/kernel/cpu/cacheinfo.c +2 -0
+/* Kernel controls MTRR and/or PAT MSRs. */
+unsigned int cache_generic;

This should either be __ro_after_init and initialized to 0 or you need accessors...

Okay.

Quoted 13 lines

arch/x86/kernel/cpu/cacheinfo.c DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);

Quoted 9 lines
arch/x86/kernel/cpu/cacheinfo.c +1 -6
  u32 num_var_ranges;
-static bool __mtrr_enabled;
-
-static bool mtrr_enabled(void)
-{
-	return __mtrr_enabled;
-}
+static bool mtrr_enabled;

Hmm, I don't like this. There's way too many boolean flags in the mtrr code. There's mtrr_state.enabled too. ;-\

Quoted 2 lines

Can we set (or clear) X86_FEATURE_MTRR to denote MTRR enablement status and get rid of one more boolean flag?

I'll have a look.

Quoted 28 lines

...

Quoted 15 lines
  void __init mtrr_bp_init(void)
  {
arch/x86/kernel/cpu/cacheinfo.c +2 -0
+	bool use_generic = false;
  	u32 phys_addr;
 
  	init_ifs();
@@ -694,6 +691,7 @@ void __init mtrr_bp_init(void)
 
  	if (boot_cpu_has(X86_FEATURE_MTRR)) {
  		mtrr_if = &generic_mtrr_ops;
+		use_generic = true;
  		size_or_mask = SIZE_OR_MASK_BITS(36);
  		size_and_mask = 0x00f00000;
  		phys_addr = 36;

arch/x86/kernel/cpu/cacheinfo.c void __init mtrr_bp_init(void) · line 757

Quoted 7 lines
arch/x86/kernel/cpu/cacheinfo.c +2 -2
@@ -755,15 +753,18 @@ void __init mtrr_bp_init(void)
  	}
 
  	if (mtrr_if) {
-		__mtrr_enabled = true;
-		set_num_var_ranges();
+		mtrr_enabled = true;
+		set_num_var_ranges(use_generic);

You don't need use_generic either:

Quoted 4 lines
		set_num_var_ranges(mtrr_if == generic_mtrr_ops);

(The reason being I wanna get rid of that nasty minefield of boolean vars all round that code).

Fine with me.

Juergen

maintainer
#6 lore raw

Re: [PATCH v3 03/10] x86/mtrr: replace use_intel() with a local flag

Quoted 4 lines · Juergen Gross

In the end this variable doesn't specify which caching types are available, but the ways to select/control the caching types.

So what about "memory_caching_select" or "memory_caching_control" instead?

_control sounds like the right thing. As in, which of the memory caching things the kernel controls. Along with a comment above it what this exactly is. Yap.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#7 lore raw

Re: [PATCH v3 03/10] x86/mtrr: replace use_intel() with a local flag

In reply to Juergen Gross
Quoted 65 lines · Juergen Gross
Quoted 13 lines · Borislav Petkov

arch/x86/include/asm/cacheinfo.h line 5

Quoted 9 lines · Juergen Gross
arch/x86/include/asm/cacheinfo.h +2 -0
index 86b2e0dcc4bf..1aeafa9888f7 100644
@@ -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;

So this should be called something more descriptive like

Quoted 1 line

    memory_caching_types

In the end this variable doesn't specify which caching types are available, but the ways to select/control the caching types.

So what about "memory_caching_select" or "memory_caching_control" instead?

Quoted 11 lines

or so to denote that this is a bitfield of supported memory caching technologies. The code then would read as

    if (memory_caching_types & CACHE_MTRR)

The name's still not optimal tho - needs more brooding over.

arch/x86/include/asm/cacheinfo.h line 8

Quoted 2 lines
arch/x86/include/asm/cacheinfo.h +2 -0
+#define CACHE_GENERIC_MTRR 0x01
+#define CACHE_GENERIC_PAT  0x02

And those should be CACHE_{MTRR,PAT}.

Fine with me.

Quoted 6 lines
Quoted 6 lines
arch/x86/include/asm/cacheinfo.h +0 -0
   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 +0 -0
index 66556833d7af..3b05d3ade7a6 100644
Quoted 8 lines

arch/x86/kernel/cpu/cacheinfo.c DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map); · line 38

Quoted 5 lines
arch/x86/kernel/cpu/cacheinfo.c +2 -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;

This should either be __ro_after_init and initialized to 0 or you need accessors...

Okay.

Quoted 13 lines

arch/x86/kernel/cpu/cacheinfo.c DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);

Quoted 9 lines
arch/x86/kernel/cpu/cacheinfo.c +1 -6
   u32 num_var_ranges;
-static bool __mtrr_enabled;
-
-static bool mtrr_enabled(void)
-{
-    return __mtrr_enabled;
-}
+static bool mtrr_enabled;

Hmm, I don't like this. There's way too many boolean flags in the mtrr code. There's mtrr_state.enabled too. ;-\

Quoted 4 lines
Quoted 2 lines

Can we set (or clear) X86_FEATURE_MTRR to denote MTRR enablement status and get rid of one more boolean flag?

I'll have a look.

Hmm, this might be a little bit risky.

It can be done, but then X86_FEATURE_MTRR could be set even for cpus NOT supporting it (the 32-bit special cases AMD, CENTAUR, CYRIX).

So we have the following alternatives:

- do the switch to X86_FEATURE_MTRR risking code breakage for later code changes querying X86_FEATURE_MTRR and assuming the MTRR MSRs being available

- keep the current bool

- replace the bool with mtrr_if != NULL

- add a new synthetic feature, e.g. X86_FEATURE_MTRR_ENABLED (which in fact would be just a replacement of the current bool)

My preference would be the replacement with mtrr_if != NULL.

Juergen

#8 lore raw

PATCH v3 4/10 x86: move some code out of arch/x86/kernel/cpu/mtrr

In reply to Juergen Gross

Prepare 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 | 81 ++++--------------------------
 4 files changed, 93 insertions(+), 72 deletions(-)
arch/x86/include/asm/cacheinfo.h +3 -0
index 1aeafa9888f7..313a6920d0f9 100644
@@ -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
index 76d726074c16..12a16caed395 100644
@@ -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 -72

143 lines not on this page · raw message

Signature · 1 line
2.35.3
maintainer
#9 lore raw

Re: [PATCH v3 04/10] x86: move some code out of arch/x86/kernel/cpu/mtrr

Quoted 2 lines · Juergen Gross

Prepare making PAT and MTRR support independent from each other by moving some code needed by both out of the MTRR specific sources.

This needs to be two patches at least: first one is only *mechanical* move without any changes. The next one(s) do the renaming and other changes etc. Otherwise reviewing it is unnecessarily complicated.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#10 lore raw

Re: [PATCH v3 04/10] x86: move some code out of arch/x86/kernel/cpu/mtrr

Quoted 7 lines · Borislav Petkov
Quoted 2 lines · Juergen Gross

Prepare making PAT and MTRR support independent from each other by moving some code needed by both out of the MTRR specific sources.

This needs to be two patches at least: first one is only *mechanical* move without any changes. The next one(s) do the renaming and other changes etc. Otherwise reviewing it is unnecessarily complicated.

Okay.

Juergen

#11 lore raw

PATCH v3 5/10 x86/mtrr: split generic_set_all()

In reply to Juergen Gross

Split 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
index 313a6920d0f9..563d9cb5fcf5 100644
@@ -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
index 12a16caed395..986249a2b9b6 100644
@@ -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
index 47e2c72fa8a4..36378604ec61 100644
@@ -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
index 5ed397f03a87..fc7b2d952737 100644
@@ -731,30 +731,19 @@ void mtrr_enable(void)
 	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_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;
 	}
-
 }
 
 /**
@@ -854,7 +843,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
maintainer
#12 lore raw

Re: [PATCH v3 05/10] x86/mtrr: split generic_set_all()

Quoted 4 lines · Juergen Gross

Split 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

"Prepare PAT support without ... "

Quoted 7 lines

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

... and so on. So the commit message should not say what you're doing - that should be visible from the diff itself. It should talk more about the *why* you're doing it.

...

Quoted 7 lines
arch/x86/kernel/cpu/cacheinfo.c +0 -0
index 47e2c72fa8a4..36378604ec61 100644
@@ -1120,3 +1120,22 @@ void cache_enable(void) __releases(cache_disable_lock)
 
 	raw_spin_unlock(&cache_disable_lock);

arch/x86/kernel/cpu/cacheinfo.c void cache_enable(void) __releases(cache_disable_lock) · line 1131

Quoted 10 lines
arch/x86/kernel/cpu/cacheinfo.c +9 -0
 }
+
+void cache_cpu_init(void)
+{
+	unsigned long flags;
+
+	local_irq_save(flags);
+	cache_disable();
+
+	/* Set MTRR state. */

Yeah, and when you name the functions properly as you've done, you don't really need those comments anymore either.

arch/x86/kernel/cpu/cacheinfo.c void cache_enable(void) __releases(cache_disable_lock) · line 1135

Quoted 4 lines
arch/x86/kernel/cpu/cacheinfo.c +4 -0
+	if (cache_generic & CACHE_GENERIC_MTRR)
+		mtrr_generic_set_state();
+
+	/* Set PAT. */

And this one.

Quoted 43 lines
arch/x86/kernel/cpu/cacheinfo.c +6 -0
+	if (cache_generic & CACHE_GENERIC_PAT)
+		pat_init();
+
+	cache_enable();
+	local_irq_restore(flags);
+}
arch/x86/kernel/cpu/mtrr/generic.c +1 -12
index 5ed397f03a87..fc7b2d952737 100644
@@ -731,30 +731,19 @@ void mtrr_enable(void)
 	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_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;
 	}
-
 }
 
 /**

arch/x86/kernel/cpu/mtrr/generic.c int positive_have_wrcomb(void) · line 846

Quoted 5 lines
arch/x86/kernel/cpu/mtrr/generic.c +1 -1
@@ -854,7 +843,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,

I was gonna say that this looks weird - a set_all function pointer assigned to a init function but that changes in the next patch.

But yeah, I like where this is going.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#13 lore raw

PATCH v3 6/10 x86/mtrr: remove set_all callback from struct mtrr_ops

In reply to Juergen Gross

Instead 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
index fc7b2d952737..5f83ee865def 100644
@@ -843,7 +843,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
index 7d7d5bd30219..9609a0d235f8 100644
@@ -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
index 88b1c4b6174a..3b1883185185 100644
@@ -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
#14 lore raw

PATCH v3 7/10 x86/mtrr: simplify mtrr_bp_init()

In reply to Juergen Gross

In 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
index 5f83ee865def..b15634e5ad44 100644
@@ -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
index 9609a0d235f8..956838bb4481 100644
@@ -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
index 3b1883185185..c98928ceee6a 100644
@@ -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
maintainer
#15 lore raw

Re: [PATCH v3 07/10] x86/mtrr: simplify mtrr_bp_init()

Quoted 7 lines · Juergen Gross
arch/x86/kernel/cpu/mtrr/mtrr.c +0 -0
index 9609a0d235f8..956838bb4481 100644
@@ -761,13 +761,10 @@ void __init mtrr_bp_init(void)
 			mtrr_enabled = get_mtrr_state();
 

arch/x86/kernel/cpu/mtrr/mtrr.c void __init mtrr_bp_init(void) · line 767

Quoted 10 lines
arch/x86/kernel/cpu/mtrr/mtrr.c +2 -5
 			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);

Just let those lines stick out.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#16 lore raw

PATCH v3 8/10 x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

In reply to Juergen Gross

In 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
index 563d9cb5fcf5..e80ed3c523c8 100644
@@ -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
index 986249a2b9b6..5d31219c8529 100644
@@ -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
index 36378604ec61..c6e7c93e45e8 100644
@@ -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
index 956838bb4481..a47d46035240 100644
@@ -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
index f24227bc3220..ef7bce21cbe8 100644
@@ -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
maintainer
#17 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

arch/x86/kernel/cpu/mtrr/mtrr.c void mtrr_save_state(void) · line 828

Quoted 8 lines · Juergen Gross
arch/x86/kernel/cpu/mtrr/mtrr.c +0 -8
-void set_mtrr_aps_delayed_init(void)
-{
-	if (!cache_generic)
-		return;
-
-	mtrr_aps_delayed_init = true;
-}
-

Except that you've removed the accessors and made that bool global. Which is less pretty than it was before...

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#18 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 13 lines · Borislav Petkov

arch/x86/kernel/cpu/mtrr/mtrr.c void mtrr_save_state(void) · line 828

Quoted 8 lines · Juergen Gross
arch/x86/kernel/cpu/mtrr/mtrr.c +0 -8
-void set_mtrr_aps_delayed_init(void)
-{
-	if (!cache_generic)
-		return;
-
-	mtrr_aps_delayed_init = true;
-}
-

Except that you've removed the accessors and made that bool global. Which is less pretty than it was before...

The accessor would now only need to set the bool, while it had at least some logic before.

TBH I don't see the point of having an accessor which is just setting a variable to "true". But if you like it better, I can keep it.

Another possibility would be to move the arch_thaw_secondary_cpus_begin() and arch_thaw_secondary_cpus_end() functions to cacheinfo.c, resulting in only a single place outside of cacheinfo.c setting the variable (in theory the arch_thaw_secondary_cpus_*() functions could just be redefined to the accessor and cache_aps_init(), but this would be rather hacky IMO).

Juergen

maintainer
#19 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 2 lines · Juergen Gross

TBH I don't see the point of having an accessor which is just setting a variable to "true". But if you like it better, I can keep it.

Accessors are always better, no matter how silly. :)

But, in trying to grok your next patch - you really should split those more complex ones because they're a pain to review - I'm starting to wonder whether we could even remove mtrr_aps_delayed_init and make the delayed init the default.

Because, AFAICT, set_mtrr_aps_delayed_init() is called by default by native_smp_prepare_cpus(). Which is called by hyperv and arch/x86/xen/smp_hvm.c.

The only one that's not calling it is arch/x86/xen/smp_pv.c but that thing doesn't support MTRRs in the first place, right?

Which means, it doesn't need delayed MTRR init anyway.

Which would then mean that this would simplify this ugly logic even more.

Or am I missing an angle?

It is possible in this nuts code.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#20 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 5 lines · Borislav Petkov
Quoted 2 lines · Juergen Gross

TBH I don't see the point of having an accessor which is just setting a variable to "true". But if you like it better, I can keep it.

Accessors are always better, no matter how silly. :)

Okay, then I'll keep it.

Quoted 11 lines

But, in trying to grok your next patch - you really should split those more complex ones because they're a pain to review - I'm starting to wonder whether we could even remove mtrr_aps_delayed_init and make the delayed init the default.

Because, AFAICT, set_mtrr_aps_delayed_init() is called by default by native_smp_prepare_cpus(). Which is called by hyperv and arch/x86/xen/smp_hvm.c.

The only one that's not calling it is arch/x86/xen/smp_pv.c but that thing doesn't support MTRRs in the first place, right?

Correct.

Quoted 5 lines

Which means, it doesn't need delayed MTRR init anyway.

Which would then mean that this would simplify this ugly logic even more.

Or am I missing an angle?

Yes: cpu hotplug.

Juergen

maintainer
#21 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 1 line · Juergen Gross

Yes: cpu hotplug.

You might need to elaborate here.

Because I see mtrr_ap_init() on the AP hotplug path:

native_cpu_up->
do_boot_cpu->
start_secondary->
smp_callin->
smp_store_cpu_info->
identify_secondary_cpu->
mtrr_ap_init

Which then means that we could check in mtrr_ap_init() if we're on the hotplug path and still get rid of that stupid bool...

Close?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#22 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 15 lines · Borislav Petkov
Quoted 1 line · Juergen Gross

Yes: cpu hotplug.

You might need to elaborate here.

Because I see mtrr_ap_init() on the AP hotplug path:

native_cpu_up->
do_boot_cpu->
start_secondary->
smp_callin->
smp_store_cpu_info->
identify_secondary_cpu->
mtrr_ap_init

Quoted 5 lines

Which then means that we could check in mtrr_ap_init() if we're on the hotplug path and still get rid of that stupid bool...

Close?

You mean by replacing it with "(system_state != SYSTEM_RUNNING)" ?

Juergen

maintainer
#23 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 1 line · Juergen Gross

You mean by replacing it with "(system_state != SYSTEM_RUNNING)" ?

Right, or maybe even something more elegant. I've been meaning to ask tglx about it as I needed it for the microcode loader too.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#24 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 5 lines · Borislav Petkov
Quoted 1 line · Juergen Gross

You mean by replacing it with "(system_state != SYSTEM_RUNNING)" ?

Right, or maybe even something more elegant. I've been meaning to ask tglx about it as I needed it for the microcode loader too.

So replacing the bool with "(system_state != SYSTEM_RUNNING)" is fine with you right now? We can later switch that to the "more elegant" solution when it shows up.

Juergen

maintainer
#25 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 3 lines · Juergen Gross

So replacing the bool with "(system_state != SYSTEM_RUNNING)" is fine with you right now? We can later switch that to the "more elegant" solution when it shows up.

Ok, I think I have something. And it was staring me straight in the face but I didn't see it: the MTRR code needs a hotplug notifier. In that notifier it can do the immediate, i.e., non-delayed init while the delayed init becomes the default, see below.

And ignore the pr_info debugging gunk pls.

mtrr_ap_init() becomes the notifier callback. It doesn't need to be called in identify_secondary_cpu() anymore as in the init case that function doesn't do anything - delayed=true - and in the hotplug case the notifier runs.

mtrr_aps_init() - "aps" in plural - does the delayed init after all CPUs have been brought online after the box has booted. That might need some renaming.

And yes, there's a lot more to cleanup after this. This code has grown wart after wart over the years...

Fun.

---

arch/x86/include/asm/mtrr.h +0 -4
index 76d726074c16..1a3dad244bba 100644
@@ -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 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);
@@ -83,8 +81,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 set_mtrr_aps_delayed_init() do {} while (0)
 #define mtrr_aps_init() do {} while (0)
 #define mtrr_bp_restore() do {} while (0)
 #  endif
arch/x86/kernel/cpu/common.c +0 -1
index 3e508f239098..deef1b5b27cc 100644
@@ -1948,7 +1948,6 @@ void identify_secondary_cpu(struct cpuinfo_x86 *c)
 #ifdef CONFIG_X86_32
 	enable_sep_cpu();
 #endif
-	mtrr_ap_init();
 	validate_apic_and_package_id(c);
 	x86_spec_ctrl_setup_ap();
 	update_srbds_msr();
arch/x86/kernel/cpu/mtrr/mtrr.c +20 -25

96 lines not on this page · raw message

arch/x86/kernel/smpboot.c +2 -2
index f24227bc3220..171acef35201 100644
@@ -1428,7 +1428,7 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)
 
 	uv_system_init();
 
-	set_mtrr_aps_delayed_init();
+	pr_info("%s: set_mtrr_aps_delayed_init\n", __func__);
 
 	smp_quirk_init_udelay();
 
@@ -1439,7 +1439,7 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)
 
 void arch_thaw_secondary_cpus_begin(void)
 {
-	set_mtrr_aps_delayed_init();
+	pr_info("%s: set_mtrr_aps_delayed_init\n", __func__);
 }
 
 void arch_thaw_secondary_cpus_end(void)
kernel/cpu.c +2 -0
index bbad5e375d3b..fc14601b908c 100644
@@ -591,6 +591,8 @@ static int bringup_cpu(unsigned int cpu)
 	struct task_struct *idle = idle_thread_get(cpu);
 	int ret;
 
+	pr_info("%s: CPU%d\n", __func__, cpu);
+
 	/*
 	 * Reset stale stack state from the last time this CPU was online.
 	 */
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#26 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 10 lines · Borislav Petkov
Quoted 3 lines · Juergen Gross

So replacing the bool with "(system_state != SYSTEM_RUNNING)" is fine with you right now? We can later switch that to the "more elegant" solution when it shows up.

Ok, I think I have something. And it was staring me straight in the face but I didn't see it: the MTRR code needs a hotplug notifier. In that notifier it can do the immediate, i.e., non-delayed init while the delayed init becomes the default, see below.

Quoted 6 lines

And ignore the pr_info debugging gunk pls.

mtrr_ap_init() becomes the notifier callback. It doesn't need to be called in identify_secondary_cpu() anymore as in the init case that function doesn't do anything - delayed=true - and in the hotplug case the notifier runs.

Are sure the hotplug notifier doesn't get called in the boot and in the resume cases? I don't see how those calls are being not done or resulting in not doing anything.

Juergen

#27 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 13 lines · Juergen Gross
Quoted 12 lines · Borislav Petkov
Quoted 3 lines · Juergen Gross

So replacing the bool with "(system_state != SYSTEM_RUNNING)" is fine with you right now? We can later switch that to the "more elegant" solution when it shows up.

Ok, I think I have something. And it was staring me straight in the face but I didn't see it: the MTRR code needs a hotplug notifier. In that notifier it can do the immediate, i.e., non-delayed init while the delayed init becomes the default, see below.

And ignore the pr_info debugging gunk pls.

Quoted 8 lines
Quoted 4 lines

mtrr_ap_init() becomes the notifier callback. It doesn't need to be called in identify_secondary_cpu() anymore as in the init case that function doesn't do anything - delayed=true - and in the hotplug case the notifier runs.

Are sure the hotplug notifier doesn't get called in the boot and in the resume cases? I don't see how those calls are being not done or resulting in not doing anything.

In case my suspicion is correct: this can still be solved by adding the hotplug notifier only in mtrr_aps_init(), and removing it again in arch_thaw_secondary_cpus_begin().

Juergen

maintainer
#28 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 1 line · Juergen Gross
Quoted 1 line

Are sure the hotplug notifier doesn't get called in the boot and in the

It doesn't because it gets registered after smp_init()...

Quoted 1 line
Quoted 1 line

resume cases?

... but it gets called during resume because by that time the notifier has been registered already. See my notes at the end of this mail of what the code does currently.

Quoted 3 lines

In case my suspicion is correct: this can still be solved by adding the hotplug notifier only in mtrr_aps_init(), and removing it again in arch_thaw_secondary_cpus_begin().

Pretty much. Yeah, we still need a bool. ;-(

But that bool has a much smaller scope and it is perfectly clear what it does. And I've added a comment. Could've used comments for the delayed init thing.

Anyway, it gets set in a thaw callback (I mean, might as well, since we call into the MTRR code anyway). I probably can make this even cleaner and not do any bool if I could query in the notifier whether I'm resuming...

Thx.

---

arch/x86/include/asm/mtrr.h +2 -4
index 76d726074c16..86b8009d2429 100644
@@ -42,9 +42,8 @@ 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 set_mtrr_aps_delayed_init(void);
 extern void mtrr_aps_init(void);
+extern void mtrr_aps_thaw(void);
 extern void mtrr_bp_restore(void);
 extern int mtrr_trim_uncached_memory(unsigned long end_pfn);
 extern int amd_special_default_mtrr(void);
@@ -83,9 +82,8 @@ 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 set_mtrr_aps_delayed_init() do {} while (0)
 #define mtrr_aps_init() do {} while (0)
+#define mtrr_aps_thaw() do {} while (0)
 #define mtrr_bp_restore() do {} while (0)
 #  endif
 
arch/x86/kernel/cpu/common.c +0 -1
index 3e508f239098..deef1b5b27cc 100644
@@ -1948,7 +1948,6 @@ void identify_secondary_cpu(struct cpuinfo_x86 *c)
 #ifdef CONFIG_X86_32
 	enable_sep_cpu();
 #endif
-	mtrr_ap_init();
 	validate_apic_and_package_id(c);
 	x86_spec_ctrl_setup_ap();
 	update_srbds_msr();
arch/x86/kernel/cpu/mtrr/mtrr.c +27 -26

100 lines not on this page · raw message

arch/x86/kernel/smpboot.c +1 -3
index f24227bc3220..b90780dab88a 100644
@@ -1428,8 +1428,6 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)
 
 	uv_system_init();
 
-	set_mtrr_aps_delayed_init();
-
 	smp_quirk_init_udelay();
 
 	speculative_store_bypass_ht_init();
@@ -1439,7 +1437,7 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)
 
 void arch_thaw_secondary_cpus_begin(void)
 {
-	set_mtrr_aps_delayed_init();
+	mtrr_aps_thaw();
 }
 
 void arch_thaw_secondary_cpus_end(void)

Notes:
------

Boot sequence:

BSP:

[    0.272801] smpboot: native_smp_prepare_cpus: set_mtrr_aps_delayed_init

APs:

[    0.287190] mtrr_save_state: first_cpu: 0
[    0.287724] x86: Booting SMP configuration:
[    0.290292] .... node  #0, CPUs:        #1
[    0.061135] mtrr_ap_init: single AP entry, use_intel: 1, mtrr_enabled: 1, mtrr_aps_delayed_init: 1

-> set_mtrr_from_inactive_cpu() gets skipped.

After all APs booted:

[    1.544506] mtrr_aps_init: entry, use_intel: 1, mtrr_enabled: 1, mtrr_aps_delayed_init: 1

-> set_mtrr()

hotplug:

[  206.112651] smpboot: CPU 11 is now offline
[  208.286030] bringup_cpu: CPU11
[  208.286611] mtrr_save_state: first_cpu: 0
[  208.287416] smpboot: Booting Node 0 Processor 11 APIC 0xb
[  206.116567] mtrr_ap_init: single AP entry, use_intel: 1, mtrr_enabled: 1, mtrr_aps_delayed_init: 0

-> set_mtrr_from_inactive_cpu()

suspend/resume:

BSP:

[  270.586643] smpboot: arch_thaw_secondary_cpus_begin: set_mtrr_aps_delayed_init

APs:

[  270.587947] bringup_cpu: CPU1
[  270.588470] mtrr_save_state: first_cpu: 0
[  270.589207] x86: Booting SMP configuration:
[  270.597971] smpboot: Booting Node 0 Processor 1 APIC 0x1
[  270.530418] mtrr_ap_init: single AP entry, use_intel: 1, mtrr_enabled: 1, mtrr_aps_delayed_init: 1

After all APs booted:

[  270.694168] mtrr_aps_init: entry, use_intel: 1, mtrr_enabled: 1, mtrr_aps_delayed_init: 1
[  270.696923] ACPI: PM: Waking up from system sleep state S3
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#29 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 11 lines · Borislav Petkov
Quoted 1 line · Juergen Gross
Quoted 1 line

Are sure the hotplug notifier doesn't get called in the boot and in the

It doesn't because it gets registered after smp_init()...

Quoted 1 line
Quoted 1 line

resume cases?

... but it gets called during resume because by that time the notifier has been registered already. See my notes at the end of this mail of what the code does currently.

Quoted 5 lines
Quoted 3 lines

In case my suspicion is correct: this can still be solved by adding the hotplug notifier only in mtrr_aps_init(), and removing it again in arch_thaw_secondary_cpus_begin().

Pretty much. Yeah, we still need a bool. ;-(

No, we don't.

Using basically your patch, but with

+ mtrr_online = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
+ "x86/mtrr:online",
+ mtrr_ap_init, NULL);

moved to the end of mtrr_aps_init(), and:

+void mtrr_aps_thaw(void)
+{
+ cpuhp_remove_state_nocalls(mtrr_online);
+}

Juergen

maintainer
#30 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 10 lines · Juergen Gross

No, we don't.

Using basically your patch, but with

(quoted diff) +3 -0
+	mtrr_online = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
+						"x86/mtrr:online",
+						mtrr_ap_init, NULL);

moved to the end of mtrr_aps_init(), and:

arch/x86/kernel/cpu/mtrr/mtrr.c void mtrr_save_state(void)

Quoted 4 lines
arch/x86/kernel/cpu/mtrr/mtrr.c +4 -0
+void mtrr_aps_thaw(void)
+{
+	cpuhp_remove_state_nocalls(mtrr_online);
+}

Yes, so you said. I'm not sure I like this toggling of notifier registration like that.

Optimally, I'd like to be able to query the suspend code whether it is in the process of resuming.

This here:

static int resume_target_kernel(bool platform_mode) {

...

Enable_irqs:

        system_state = SYSTEM_RUNNING;
        local_irq_enable();

Enable_cpus:

        pm_sleep_enable_secondary_cpus();

but being able to do:

        pm_sleep_enable_secondary_cpus();
	system_state = SYSTEM_RUNNING | SYSTEM_RUNNING_APS_UP;

which can't work, obviously. But something like that.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#31 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 11 lines · Borislav Petkov
Quoted 10 lines · Juergen Gross

No, we don't.

Using basically your patch, but with

(quoted diff) +3 -0
+	mtrr_online = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
+						"x86/mtrr:online",
+						mtrr_ap_init, NULL);

moved to the end of mtrr_aps_init(), and:

Quoted 7 lines

arch/x86/kernel/cpu/mtrr/mtrr.c void mtrr_save_state(void)

Quoted 4 lines
arch/x86/kernel/cpu/mtrr/mtrr.c +4 -0
+void mtrr_aps_thaw(void)
+{
+	cpuhp_remove_state_nocalls(mtrr_online);
+}

Yes, so you said. I'm not sure I like this toggling of notifier registration like that.

Yeah, especially with having to remember the slot.

Would you feel better with adding a new enum member CPUHP_AP_CACHECTRL_ONLINE?

This would avoid a possible source of failure during resume in case no slot for CPUHP_AP_ONLINE_DYN is found (quite improbable, but in theory possible).

Quoted 19 lines

Optimally, I'd like to be able to query the suspend code whether it is in the process of resuming.

This here:

static int resume_target_kernel(bool platform_mode) {

...

  Enable_irqs:
         system_state = SYSTEM_RUNNING;
         local_irq_enable();
  
  Enable_cpus:
         pm_sleep_enable_secondary_cpus();
Quoted 7 lines

but being able to do:

         pm_sleep_enable_secondary_cpus();
	system_state = SYSTEM_RUNNING | SYSTEM_RUNNING_APS_UP;

which can't work, obviously. But something like that.

You wouldn't want to do that there, as there are multiple places where pm_sleep_enable_secondary_cpus() is being called. Additionally not all cases are coming in via pm_sleep_enable_secondary_cpus(), as there is e.g. a call of suspend_enable_secondary_cpus() from kernel_kexec(), which wants to have the same handling.

arch_thaw_secondary_cpus_begin() and arch_thaw_secondary_cpus_end() are the functions to mark start and end of the special region where the delayed MTRR setup should happen.

Juergen

maintainer
#32 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 4 lines · Juergen Gross

Would you feel better with adding a new enum member CPUHP_AP_CACHECTRL_ONLINE?

This would avoid a possible source of failure during resume in case no slot for CPUHP_AP_ONLINE_DYN is found (quite improbable, but in theory possible).

Let's keep that in the bag for the time when we get to cross that bridge.

Quoted 2 lines

You wouldn't want to do that there, as there are multiple places where pm_sleep_enable_secondary_cpus() is being called.

We want all of them, I'd say. They're all some sort of suspend AFAICT. But yes, if we get to do it, that would need a proper audit.

Quoted 4 lines

Additionally not all cases are coming in via pm_sleep_enable_secondary_cpus(), as there is e.g. a call of suspend_enable_secondary_cpus() from kernel_kexec(), which wants to have the same handling.

Which means, more hairy.

Quoted 3 lines

arch_thaw_secondary_cpus_begin() and arch_thaw_secondary_cpus_end() are the functions to mark start and end of the special region where the delayed MTRR setup should happen.

Yap, it seems like the best solution at the moment. Want me to do a proper patch and test it on real hw?

:-)

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#33 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 19 lines · Borislav Petkov
Quoted 4 lines · Juergen Gross

Would you feel better with adding a new enum member CPUHP_AP_CACHECTRL_ONLINE?

This would avoid a possible source of failure during resume in case no slot for CPUHP_AP_ONLINE_DYN is found (quite improbable, but in theory possible).

Let's keep that in the bag for the time when we get to cross that bridge.

Quoted 2 lines

You wouldn't want to do that there, as there are multiple places where pm_sleep_enable_secondary_cpus() is being called.

We want all of them, I'd say. They're all some sort of suspend AFAICT. But yes, if we get to do it, that would need a proper audit.

Quoted 4 lines

Additionally not all cases are coming in via pm_sleep_enable_secondary_cpus(), as there is e.g. a call of suspend_enable_secondary_cpus() from kernel_kexec(), which wants to have the same handling.

Quoted 8 lines

Which means, more hairy.

Quoted 3 lines

arch_thaw_secondary_cpus_begin() and arch_thaw_secondary_cpus_end() are the functions to mark start and end of the special region where the delayed MTRR setup should happen.

Yap, it seems like the best solution at the moment. Want me to do a proper patch and test it on real hw?

I can do that.

Juergen

maintainer
#34 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 1 line · Juergen Gross

I can do that.

Thx!

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#35 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

In reply to Juergen Gross
Quoted 22 lines · Juergen Gross
Quoted 21 lines · Borislav Petkov
Quoted 4 lines · Juergen Gross

Would you feel better with adding a new enum member CPUHP_AP_CACHECTRL_ONLINE?

This would avoid a possible source of failure during resume in case no slot for CPUHP_AP_ONLINE_DYN is found (quite improbable, but in theory possible).

Let's keep that in the bag for the time when we get to cross that bridge.

Quoted 2 lines

You wouldn't want to do that there, as there are multiple places where pm_sleep_enable_secondary_cpus() is being called.

We want all of them, I'd say. They're all some sort of suspend AFAICT. But yes, if we get to do it, that would need a proper audit.

Quoted 4 lines

Additionally not all cases are coming in via pm_sleep_enable_secondary_cpus(), as there is e.g. a call of suspend_enable_secondary_cpus() from kernel_kexec(), which wants to have the same handling.

Which means, more hairy.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

arch_thaw_secondary_cpus_begin() and arch_thaw_secondary_cpus_end() are the functions to mark start and end of the special region where the delayed MTRR setup should happen.

Yap, it seems like the best solution at the moment. Want me to do a proper patch and test it on real hw?

I can do that.

Okay, lets define what is meant by "that" just to be on the same page.

The idea to use a hotplug callback seems to be rather risky IMHO. At least CPUHP_AP_ONLINE_DYN seems to be way too late, as there are several device drivers hooking in with the same or lower priority already. And device drivers might rely on PAT settings in PTEs of MTRR being setup correctly.

Another problematic case is CPUHP_AP_MICROCODE_LOADER, which is explicitly doing cache writeback and invalidation, which seems to be risky without having a sane PAT/MTRR state of the processor. It should be noted that the microcode loader is registered via late_initcall(), so boot isn't affected by the delayed MTRR/PAT init when booting.

So the only secure way to use a hotplug callback would be to have a rather early preregistered slot in enum cpuhp_state.

Regarding resume and kexec I'm no longer sure doing the delayed MTRR/PAT init is such a great idea. It might save some milliseconds, but the risks mentioned above with e.g. microcode loading should apply.

So right now I'm inclined to be better on the safe side by not adding any cpu hotplug hook, but to use just the same "delayed AP init" flag as today, just renaming it. This would leave the delayed MTRR/PAT init in place for resume and kexec cases, but deferring the MTRR/PAT cleanup due to this potential issue seems not appropriate, as the cleanup isn't changing the behavior here.

We should, however, have a discussion in parallel or later, whether the whole thaw_secondary_cpus() handling is really okay or whether it should be changed in some way.

Juergen

maintainer
#36 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 6 lines · Juergen Gross

So right now I'm inclined to be better on the safe side by not adding any cpu hotplug hook, but to use just the same "delayed AP init" flag as today, just renaming it. This would leave the delayed MTRR/PAT init in place for resume and kexec cases, but deferring the MTRR/PAT cleanup due to this potential issue seems not appropriate, as the cleanup isn't changing the behavior here.

Ok, what's wrong with adding a special hotplug level just for that thing and running it very early? Practically pretty much where it was in time, in identify_secondary_cpu()?

Having a special one is warranted, as you explain, I'd say.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#37 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 11 lines · Borislav Petkov
Quoted 6 lines · Juergen Gross

So right now I'm inclined to be better on the safe side by not adding any cpu hotplug hook, but to use just the same "delayed AP init" flag as today, just renaming it. This would leave the delayed MTRR/PAT init in place for resume and kexec cases, but deferring the MTRR/PAT cleanup due to this potential issue seems not appropriate, as the cleanup isn't changing the behavior here.

Ok, what's wrong with adding a special hotplug level just for that thing and running it very early? Practically pretty much where it was in time, in identify_secondary_cpu()?

Yes, this can be done. It would practically have to be the first one just after CPUHP_BRINGUP_CPU.

The question is whether we really want to call the MTRR/PAT initialization on hotplugged cpus only after enabling interrupts. Note that the callbacks are activated only at the end of start_secondary(), while today MTRR/PAT initialization is called some time earlier by:

start_secondary()

    smp_callin()
      smp_store_cpu_info()
        identify_secondary_cpu()
          mtrr_ap_init()

I don't think this is a real problem, but I wanted to mention it.

The next question would be, why MTRR/PAT init should be special (meaning:
why are all the other functions called that early not realized via callbacks)? Is it just because of the special handling during boot/resume?

It might be worth a discussion whether there shouldn't be a special group of callbacks activated BEFORE interrupts are being enabled.

Quoted 1 line

Having a special one is warranted, as you explain, I'd say.

Thanks. I'll write a patch for that.

Juergen

maintainer
#38 lore raw

Re: [PATCH v3 08/10] x86/mtrr: let cache_aps_delayed_init replace mtrr_aps_delayed_init

Quoted 2 lines · Juergen Gross

Yes, this can be done. It would practically have to be the first one just after CPUHP_BRINGUP_CPU.

Right.

Quoted 5 lines

The question is whether we really want to call the MTRR/PAT initialization on hotplugged cpus only after enabling interrupts. Note that the callbacks are activated only at the end of start_secondary(), while today MTRR/PAT initialization is called some time earlier by:

Quoted 7 lines

start_secondary()

    smp_callin()
      smp_store_cpu_info()
        identify_secondary_cpu()
          mtrr_ap_init()

I don't think this is a real problem, but I wanted to mention it.

Yep, I saw that too but I don't think there will be a problem either. I mean, it should be early enough as you point out not to need proper MTRR/PAT settings yet.

But we'll make sure we test this real good too.

Quoted 3 lines

The next question would be, why MTRR/PAT init should be special (meaning: why are all the other functions called that early not realized via callbacks)?

Well, our init code is crazy. Frankly, I don't see why not more of the "init stuff on the freshly hotplugged CPU" work is done there...

Quoted 1 line

Is it just because of the special handling during boot/resume?

... unless this is the case, ofc. Right.

Quoted 2 lines

It might be worth a discussion whether there shouldn't be a special group of callbacks activated BEFORE interrupts are being enabled.

That's a good question. /me writes it down to ask tglx when he gets back.

I mean, that early I don't think it matters whether IRQs are enabled or not. But this'll need to be audited on a case by case basis. As I said, our boot code is nuts with stuff bolted on everywhere for whatever reasons.

Quoted 1 line

Thanks. I'll write a patch for that.

Thanks too.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#39 lore raw

PATCH v3 9/10 x86/mtrr: add a stop_machine() handler calling only cache_cpu_init()

In reply to Juergen Gross

Instead 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
index e80ed3c523c8..a122a1aad936 100644
@@ -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
index 5d31219c8529..ec73d1e5bafb 100644
@@ -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
index 3e508f239098..fd058b547f8d 100644
@@ -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>
@@ -1948,7 +1949,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
index 216fee7144ee..e0e185ee0229 100644
@@ -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
index ef7bce21cbe8..ff793f436904 100644
@@ -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
index bb176c72891c..754221c9a1c3 100644
@@ -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
#40 lore raw

PATCH v3 10/10 x86: decouple pat and mtrr handling

In reply to Juergen Gross

Today 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
index 9ca760e430b9..113b2fa51849 100644
@@ -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
index 4946f93eb16f..08130919d55d 100644
@@ -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
index 5e8be11d1873..921f425fe7b3 100644
@@ -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
index e0e185ee0229..aacaa96f0195 100644
@@ -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