Skip to contentSkip to conversation

Speed MTRR programming up when we can

PATCH Superseded Ricardo Neri 2 participants 6 messages Jun 27, 2019 – Jun 28, 2019 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Speed MTRR programming up when we can

Programming MTRR registers in multi-processor systems is a rather lengthy process. Furthermore, all processors must program these registers in lock step and with interrupts disabled; the process also involves flushing caches and TLBs twice. As a result, the process may take a considerable amount of time.

In some platforms, this can lead to a large skew of the refined-jiffies clock source. Early when booting, if no other clock is available (e.g., booting with hpet=disabled), the refined-jiffies clock source is used to monitor the TSC clock source. If the skew of refined-jiffies is too large, Linux wrongly assumes that the TSC is unstable:

clocksource: timekeeping watchdog on CPU1: Marking clocksource
'tsc-early' as unstable because the skew is too large:
clocksource: 'refined-jiffies' wd_now: fffedc10 wd_last:
fffedb90 mask: ffffffff
clocksource: 'tsc-early' cs_now: 5eccfddebc cs_last: 5e7e3303d4
mask: ffffffffffffffff
tsc: Marking TSC unstable due to clocksource watchdog

As per my measurements, around 98% of the time needed by the procedure to program MTRRs in multi-processor systems is spent flushing caches with wbinvd(). As per the Section 11.11.8 of the Intel 64 and IA 32 Architectures Software Developer's Manual, it is not necessary to flush caches if the CPU supports cache self-snooping. Thus, skipping the cache flushes can reduce by several tens of milliseconds the time needed to complete the programming of the MTRR registers.

By measuring the execution time of mtrr_aps_init() (from which MTRRs in all CPUs are programmed in lock-step at boot), I find savings in the time required to program MTRRs as follows:

Platform time-with-wbinvd(ms) time-no-wbinvd(ms)
104-core (208 LP) Skylake 1437 28
2-core (4 LP) Haswell 114 2

However, there exist CPU models with errata that affect their self- snooping capability. Such errata may cause unpredictable behavior, machine check errors, or hangs. For instance:

"Where two different logical processors have the same code page
mapped with two different memory types Specifically if one code page is mapped by one logical processor as write back and by another as uncacheable and certain instruction timing conditions occur the system may experience unpredictable behaviour." [1].

Similar errata are reported in other processors as well [2], [3], [4], [5], and [6].

Thus, in order to confidently leverage self-snooping for the MTRR programming algorithm, we must first clear such feature in models with known errata.

Thanks and BR,
Ricardo

LP = Logical Processor

[1]. Erratum BF52,
https://www.intel.com/content/dam/www/public/us/en/documents/specification-updates/xeon-3600-specification-update.pdf
[2]. Erratum BK47,
https://www.mouser.com/pdfdocs/2ndgencorefamilymobilespecificationupdate.pdf
[3]. Erratum AAO54,
https://www.intel.com/content/dam/www/public/us/en/documents/specification-updates/xeon-c5500-c3500-spec-update.pdf
[4]. Errata AZ39, AZ42,
https://www.intel.com/content/dam/support/us/en/documents/processors/mobile/celeron/sb/320121.pdf
[5]. Errata AQ51, AQ102, AQ104,
https://www.intel.com/content/dam/www/public/us/en/documents/specification-updates/pentium-dual-core-desktop-e2000-specification-update.pdf
[6]. Errata AN107, AN109,
https://www.intel.com/content/dam/www/public/us/en/documents/specification-updates/pentium-dual-core-specification-update.pdf

Ricardo Neri (2):
x86/cpu/intel: Clear cache self-snoop capability in CPUs with known
errata
x86, mtrr: generic: Skip cache flushes on CPUs with cache
self-snooping

 arch/x86/kernel/cpu/intel.c        | 30 ++++++++++++++++++++++++++++++
 arch/x86/kernel/cpu/mtrr/generic.c |  8 ++++++--
 2 files changed, 36 insertions(+), 2 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 x86/cpu/intel: Clear cache self-snoop capability in CPUs with known errata

Processors which have self-snooping capability can handle conflicting memory type across CPUs by snooping its own cache. However, there exists CPU models in which having conflicting memory types still leads to unpredictable behavior, machine check errors, or hangs. Clear this feature to prevent its use. For instance, the algorithm to program the Memory Type Region Registers and the Page Attribute Table MSR can skip expensive cache flushes if self-snooping is supported.

Cc Tony Luck <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Hans de Goede <[email protected]>
Cc Peter Feiner <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Jordan Borgner <[email protected]>
Cc "Ravi V. Shankar" <[email protected]>
---
 arch/x86/kernel/cpu/intel.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
arch/x86/kernel/cpu/intel.c +30 -0
index f17c1a714779..8684c66e71e0 100644
@@ -66,6 +66,34 @@ void check_mpx_erratum(struct cpuinfo_x86 *c)
 	}
 }
 
+/*
+ * Processors which have self-snooping capability can handle conflicting
+ * memory type across CPUs by snooping its own cache. However, there exists
+ * CPU models in which having conflicting memory types still leads to
+ * unpredictable behavior, machine check errors, or hangs. Clear this feature
+ * to prevent its use. For instance, the algorithm to program the Memory Type
+ * Region Registers and the Page Attribute Table MSR can skip expensive cache
+ * flushes if self-snooping is supported.
+ */
+static void check_memory_type_self_snoop_errata(struct cpuinfo_x86 *c)
+{
+	switch (c->x86_model) {
+	case INTEL_FAM6_CORE_YONAH:
+	case INTEL_FAM6_CORE2_MEROM:
+	case INTEL_FAM6_CORE2_MEROM_L:
+	case INTEL_FAM6_CORE2_PENRYN:
+	case INTEL_FAM6_CORE2_DUNNINGTON:
+	case INTEL_FAM6_NEHALEM:
+	case INTEL_FAM6_NEHALEM_G:
+	case INTEL_FAM6_NEHALEM_EP:
+	case INTEL_FAM6_NEHALEM_EX:
+	case INTEL_FAM6_WESTMERE:
+	case INTEL_FAM6_WESTMERE_EP:
+	case INTEL_FAM6_SANDYBRIDGE:
+		setup_clear_cpu_cap(X86_FEATURE_SELFSNOOP);
+	}
+}
+
 static bool ring3mwait_disabled __read_mostly;
 
 static int __init ring3mwait_disable(char *__unused)
@@ -311,6 +339,8 @@ static void early_init_intel(struct cpuinfo_x86 *c)
 	 */
 	if (detect_extended_topology_early(c) < 0)
 		detect_ht_early(c);
+
+	check_memory_type_self_snoop_errata(c);
 }
 
 #ifdef CONFIG_X86_32
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 1/2] x86/cpu/intel: Clear cache self-snoop capability in CPUs with known errata

Ricardo,

Quoted 9 lines · Ricardo Neri

+/*
+ * Processors which have self-snooping capability can handle conflicting
+ * memory type across CPUs by snooping its own cache. However, there exists
+ * CPU models in which having conflicting memory types still leads to
+ * unpredictable behavior, machine check errors, or hangs. Clear this feature
+ * to prevent its use. For instance, the algorithm to program the Memory Type
+ * Region Registers and the Page Attribute Table MSR can skip expensive cache
+ * flushes if self-snooping is supported.

I appreciate informative comments, but this is the part which disables a feature on errata inflicted CPUs. So the whole information about what self-snooping helps with is not that interesting here. It's broken, we disable it and be done with it.

Quoted 10 lines

+ */

arch/x86/kernel/cpu/intel.c +9 -0
+static void check_memory_type_self_snoop_errata(struct cpuinfo_x86 *c)
+{
+	switch (c->x86_model) {
+	case INTEL_FAM6_CORE_YONAH:
+	case INTEL_FAM6_CORE2_MEROM:
+	case INTEL_FAM6_CORE2_MEROM_L:
+	case INTEL_FAM6_CORE2_PENRYN:
+	case INTEL_FAM6_CORE2_DUNNINGTON:
+	case INTEL_FAM6_NEHALEM:

arch/x86/kernel/cpu/intel.c void check_mpx_erratum(struct cpuinfo_x86 *c) · line 96

Quoted 10 lines
arch/x86/kernel/cpu/intel.c +10 -0
+	case INTEL_FAM6_NEHALEM_G:
+	case INTEL_FAM6_NEHALEM_EP:
+	case INTEL_FAM6_NEHALEM_EX:
+	case INTEL_FAM6_WESTMERE:
+	case INTEL_FAM6_WESTMERE_EP:
+	case INTEL_FAM6_SANDYBRIDGE:
+		setup_clear_cpu_cap(X86_FEATURE_SELFSNOOP);
+	}
+}
+

But looking at the actual interesting part of the 2nd patch:

Review static void prepare_set(void) __acquires(set_atomicity_lock) · line 748

Quoted 8 lines
(quoted diff) +3 -1
@@ -743,7 +743,9 @@ static void prepare_set(void) __acquires(set_atomicity_lock)
       /* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
       cr0 = read_cr0() | X86_CR0_CD;
       write_cr0(cr0);
-       wbinvd();
+
+       if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
+               wbinvd();

This part lacks any form of explanation. So I'd rather have the comment about why we can avoid the wbindv() here. I''d surely never would look at that errata handling function to get that information.

Other than that detail, the patches are well done!

Thanks,

tglx

#3 lore raw

Re: [PATCH 1/2] x86/cpu/intel: Clear cache self-snoop capability in CPUs with known errata

Quoted 13 lines · Thomas Gleixner

Ricardo,

Quoted 9 lines · Ricardo Neri

+/*
+ * Processors which have self-snooping capability can handle conflicting
+ * memory type across CPUs by snooping its own cache. However, there exists
+ * CPU models in which having conflicting memory types still leads to
+ * unpredictable behavior, machine check errors, or hangs. Clear this feature
+ * to prevent its use. For instance, the algorithm to program the Memory Type
+ * Region Registers and the Page Attribute Table MSR can skip expensive cache
+ * flushes if self-snooping is supported.

Quoted 4 lines

I appreciate informative comments, but this is the part which disables a feature on errata inflicted CPUs. So the whole information about what self-snooping helps with is not that interesting here. It's broken, we disable it and be done with it.

Sure, Thomas. I will move the the usefulness of self-snooping to the MTRR programming function as you mention below.

Quoted 37 lines
Quoted 11 lines

+ */

arch/x86/kernel/cpu/intel.c +9 -0
+static void check_memory_type_self_snoop_errata(struct cpuinfo_x86 *c)
+{
+	switch (c->x86_model) {
+	case INTEL_FAM6_CORE_YONAH:
+	case INTEL_FAM6_CORE2_MEROM:
+	case INTEL_FAM6_CORE2_MEROM_L:
+	case INTEL_FAM6_CORE2_PENRYN:
+	case INTEL_FAM6_CORE2_DUNNINGTON:
+	case INTEL_FAM6_NEHALEM:

arch/x86/kernel/cpu/intel.c void check_mpx_erratum(struct cpuinfo_x86 *c) · line 96

Quoted 10 lines
arch/x86/kernel/cpu/intel.c +10 -0
+	case INTEL_FAM6_NEHALEM_G:
+	case INTEL_FAM6_NEHALEM_EP:
+	case INTEL_FAM6_NEHALEM_EX:
+	case INTEL_FAM6_WESTMERE:
+	case INTEL_FAM6_WESTMERE_EP:
+	case INTEL_FAM6_SANDYBRIDGE:
+		setup_clear_cpu_cap(X86_FEATURE_SELFSNOOP);
+	}
+}
+

But looking at the actual interesting part of the 2nd patch:

Review static void prepare_set(void) __acquires(set_atomicity_lock) · line 748

Quoted 8 lines
(quoted diff) +3 -1
@@ -743,7 +743,9 @@ static void prepare_set(void) __acquires(set_atomicity_lock)
       /* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
       cr0 = read_cr0() | X86_CR0_CD;
       write_cr0(cr0);
-       wbinvd();
+
+       if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
+               wbinvd();

This part lacks any form of explanation. So I'd rather have the comment about why we can avoid the wbindv() here. I''d surely never would look at that errata handling function to get that information.

Quoted 1 line

Other than that detail, the patches are well done!

Thank you, Thomas!

BR,
Ricardo

#4 lore raw

PATCH 2/2 x86, mtrr: generic: Skip cache flushes on CPUs with cache self-snooping

In reply to Ricardo Neri

Programming MTRR registers in multi-processor systems is a rather lengthy process. Furthermore, all processors must program these registers in lock step and with interrupts disabled; the process also involves flushing caches and TLBs twice. As a result, the process may take a considerable amount of time.

In some platforms, this can lead to a large skew of the refined-jiffies clock source. Early when booting, if no other clock is available (e.g., booting with hpet=disabled), the refined-jiffies clock source is used to monitor the TSC clock source. If the skew of refined-jiffies is too large, Linux wrongly assumes that the TSC is unstable:

clocksource: timekeeping watchdog on CPU1: Marking clocksource
'tsc-early' as unstable because the skew is too large:
clocksource: 'refined-jiffies' wd_now: fffedc10 wd_last:
fffedb90 mask: ffffffff
clocksource: 'tsc-early' cs_now: 5eccfddebc cs_last: 5e7e3303d4
mask: ffffffffffffffff
tsc: Marking TSC unstable due to clocksource watchdog

As per my measurements, around 98% of the time needed by the procedure to program MTRRs in multi-processor systems is spent flushing caches with wbinvd(). As per the Section 11.11.8 of the Intel 64 and IA 32 Architectures Software Developer's Manual, it is not necessary to flush caches if the CPU supports cache self-snooping. Thus, skipping the cache flushes can reduce by several tens of milliseconds the time needed to complete the programming of the MTRR registers.

Cc Alan Cox <[email protected]>
Cc Tony Luck <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Hans de Goede <[email protected]>
Cc Peter Feiner <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Jordan Borgner <[email protected]>
Cc "Ravi V. Shankar" <[email protected]>
---
 arch/x86/kernel/cpu/mtrr/generic.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/mtrr/generic.c +6 -2
index 9356c1c9024d..169672a6935c 100644
@@ -743,7 +743,9 @@ static void prepare_set(void) __acquires(set_atomicity_lock)
 	/* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
 	cr0 = read_cr0() | X86_CR0_CD;
 	write_cr0(cr0);
-	wbinvd();
+
+	if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
+		wbinvd();
 
 	/* Save value of CR4 and clear Page Global Enable (bit 7) */
 	if (boot_cpu_has(X86_FEATURE_PGE)) {
@@ -760,7 +762,9 @@ static void prepare_set(void) __acquires(set_atomicity_lock)
 
 	/* Disable MTRRs, and set the default type to uncached */
 	mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
-	wbinvd();
+
+	if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
+		wbinvd();
 }
 
 static void post_set(void) __releases(set_atomicity_lock)
Signature · 1 line
2.17.1
#5 lore raw
In reply to Ricardo Neri
Quoted 7 lines · Ricardo Neri

By measuring the execution time of mtrr_aps_init() (from which MTRRs in all CPUs are programmed in lock-step at boot), I find savings in the time required to program MTRRs as follows:

Platform time-with-wbinvd(ms) time-no-wbinvd(ms)
104-core (208 LP) Skylake 1437 28
2-core (4 LP) Haswell 114 2

Impressive!