Skip to contentSkip to conversation

x86/intel: ensure Global Performance Counter Control is setup correctly

PATCH Superseded Roger Pau Monne 2 participants 15 messages Jan 11, 2024 – Jan 12, 2024

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/intel: ensure Global Performance Counter Control is setup correctly

When Architectural Performance Monitoring is available, the PERF_GLOBAL_CTRL MSR contains per-counter enable bits that is ANDed with the enable bit in the counter EVNTSEL MSR in order for a PMC counter to be enabled.

So far the watchdog code seems to have relied on the PERF_GLOBAL_CTRL enable bits being set by default, but at least on some Intel Sapphire and Emerald Rapids this is no longer the case, and Xen reports:

Testing NMI watchdog on all CPUs: 0 40 stuck

The first CPU on each package is started with PERF_GLOBAL_CTRL zeroed, so PMC0 doesn't start counting when the enable bit in EVNTSEL0 is set, due to the relevant enable bit in PERF_GLOBAL_CTRL not being set.

Check and adjust PERF_GLOBAL_CTRL during CPU initialization so that all the general-purpose PMCs are enabled. Doing so brings the state of the package-BSP PERF_GLOBAL_CTRL in line with the rest of the CPUs on the system.

---
Changes since v1:
- Do the adjustment of PERF_GLOBAL_CTRL even if the watchdog is not used, and enable all counters.
---
Unsure whether printing a warning if the value of PERF_GLOBAL_CTRL is not correct is of any value, hence I haven't added it.

---
 xen/arch/x86/cpu/intel.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
xen/arch/x86/cpu/intel.c +17 -1
index dfee64689ffe..40d3eb0e18a7 100644
@@ -533,9 +533,25 @@ static void cf_check init_intel(struct cpuinfo_x86 *c)
 	init_intel_cacheinfo(c);
 	if (c->cpuid_level > 9) {
 		unsigned eax = cpuid_eax(10);
+		unsigned int cnt = (uint8_t)(eax >> 8);
+
 		/* Check for version and the number of counters */
-		if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
+		if ((eax & 0xff) && (cnt > 1) && (cnt <= 32)) {
+			uint64_t global_ctrl;
+			unsigned int cnt_mask = (1UL << cnt) - 1;
+
+			/*
+			 * On (some?) Sapphire/Emerald Rapids platforms each
+			 * package-BSP starts with all the enable bits for the
+			 * general-purpose PMCs cleared.  Adjust so counters
+			 * can be enabled from EVNTSEL.
+			 */
+			rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl);
+			if ((global_ctrl & cnt_mask) != cnt_mask)
+				wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
+				       global_ctrl | cnt_mask);
 			__set_bit(X86_FEATURE_ARCH_PERFMON, c->x86_capability);
+		}
 	}
 
 	if ( !cpu_has(c, X86_FEATURE_XTOPOLOGY) )
Signature · 1 line
2.43.0
#1 lore raw
Quoted 35 lines · Roger Pau Monne

When Architectural Performance Monitoring is available, the PERF_GLOBAL_CTRL MSR contains per-counter enable bits that is ANDed with the enable bit in the counter EVNTSEL MSR in order for a PMC counter to be enabled.

So far the watchdog code seems to have relied on the PERF_GLOBAL_CTRL enable bits being set by default, but at least on some Intel Sapphire and Emerald Rapids this is no longer the case, and Xen reports:

Testing NMI watchdog on all CPUs: 0 40 stuck

The first CPU on each package is started with PERF_GLOBAL_CTRL zeroed, so PMC0 doesn't start counting when the enable bit in EVNTSEL0 is set, due to the relevant enable bit in PERF_GLOBAL_CTRL not being set.

Check and adjust PERF_GLOBAL_CTRL during CPU initialization so that all the general-purpose PMCs are enabled. Doing so brings the state of the package-BSP PERF_GLOBAL_CTRL in line with the rest of the CPUs on the system.

---
Changes since v1:
- Do the adjustment of PERF_GLOBAL_CTRL even if the watchdog is not used, and enable all counters.
---
Unsure whether printing a warning if the value of PERF_GLOBAL_CTRL is not correct is of any value, hence I haven't added it.

---
 xen/arch/x86/cpu/intel.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
xen/arch/x86/cpu/intel.c +0 -0
index dfee64689ffe..40d3eb0e18a7 100644

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 536

Quoted 4 lines
xen/arch/x86/cpu/intel.c +1 -0
@@ -533,9 +533,25 @@ static void cf_check init_intel(struct cpuinfo_x86 *c)
 	init_intel_cacheinfo(c);
 	if (c->cpuid_level > 9) {
 		unsigned eax = cpuid_eax(10);
+		unsigned int cnt = (uint8_t)(eax >> 8);

(Not just) since ./CODING_STYLE wants us to avoid fixed width types where possible, personally I'd prefer "& 0xff" here.

Quoted 6 lines
xen/arch/x86/cpu/intel.c +4 -1
 		/* Check for version and the number of counters */
-		if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
+		if ((eax & 0xff) && (cnt > 1) && (cnt <= 32)) {
+			uint64_t global_ctrl;
+			unsigned int cnt_mask = (1UL << cnt) - 1;
+

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 552

Quoted 10 lines
xen/arch/x86/cpu/intel.c +10 -0
+			/*
+			 * On (some?) Sapphire/Emerald Rapids platforms each
+			 * package-BSP starts with all the enable bits for the
+			 * general-purpose PMCs cleared.  Adjust so counters
+			 * can be enabled from EVNTSEL.
+			 */
+			rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl);
+			if ((global_ctrl & cnt_mask) != cnt_mask)
+				wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
+				       global_ctrl | cnt_mask);

Should there perhaps be a log message?

Jan

Quoted 5 lines
xen/arch/x86/cpu/intel.c +1 -0
 			__set_bit(X86_FEATURE_ARCH_PERFMON, c->x86_capability);
+		}
 	}
 
 	if ( !cpu_has(c, X86_FEATURE_XTOPOLOGY) )
#2 lore raw
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Roger Pau Monne

When Architectural Performance Monitoring is available, the PERF_GLOBAL_CTRL MSR contains per-counter enable bits that is ANDed with the enable bit in the counter EVNTSEL MSR in order for a PMC counter to be enabled.

So far the watchdog code seems to have relied on the PERF_GLOBAL_CTRL enable bits being set by default, but at least on some Intel Sapphire and Emerald Rapids this is no longer the case, and Xen reports:

Testing NMI watchdog on all CPUs: 0 40 stuck

The first CPU on each package is started with PERF_GLOBAL_CTRL zeroed, so PMC0 doesn't start counting when the enable bit in EVNTSEL0 is set, due to the relevant enable bit in PERF_GLOBAL_CTRL not being set.

Check and adjust PERF_GLOBAL_CTRL during CPU initialization so that all the general-purpose PMCs are enabled. Doing so brings the state of the package-BSP PERF_GLOBAL_CTRL in line with the rest of the CPUs on the system.

---
Changes since v1:
- Do the adjustment of PERF_GLOBAL_CTRL even if the watchdog is not used, and enable all counters.
---
Unsure whether printing a warning if the value of PERF_GLOBAL_CTRL is not correct is of any value, hence I haven't added it.

---
 xen/arch/x86/cpu/intel.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
Quoted 12 lines

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 536

Quoted 9 lines
xen/arch/x86/cpu/intel.c +1 -0
index dfee64689ffe..40d3eb0e18a7 100644
@@ -533,9 +533,25 @@ static void cf_check init_intel(struct cpuinfo_x86 *c)
 	init_intel_cacheinfo(c);
 	if (c->cpuid_level > 9) {
 		unsigned eax = cpuid_eax(10);
+		unsigned int cnt = (uint8_t)(eax >> 8);

(Not just) since ./CODING_STYLE wants us to avoid fixed width types where possible, personally I'd prefer "& 0xff" here.

Hm, OK. I got confused and somehow was under the impression we prefer to truncate using fixed types rather than masks.

Quoted 10 lines
Quoted 10 lines
xen/arch/x86/cpu/intel.c +8 -1
 		/* Check for version and the number of counters */
-		if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
+		if ((eax & 0xff) && (cnt > 1) && (cnt <= 32)) {
+			uint64_t global_ctrl;
+			unsigned int cnt_mask = (1UL << cnt) - 1;
+
+			/*
+			 * On (some?) Sapphire/Emerald Rapids platforms each
+			 * package-BSP starts with all the enable bits for the
+			 * general-purpose PMCs cleared.  Adjust so counters
Quoted 8 lines

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 552

Quoted 6 lines
xen/arch/x86/cpu/intel.c +6 -0
+			 * can be enabled from EVNTSEL.
+			 */
+			rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl);
+			if ((global_ctrl & cnt_mask) != cnt_mask)
+				wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
+				       global_ctrl | cnt_mask);

Should there perhaps be a log message?

I had a post-commit remark about that exactly. I can add one.

Thanks, Roger.

#3 lore raw
Quoted 45 lines · Roger Pau Monné
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Roger Pau Monne

When Architectural Performance Monitoring is available, the PERF_GLOBAL_CTRL MSR contains per-counter enable bits that is ANDed with the enable bit in the counter EVNTSEL MSR in order for a PMC counter to be enabled.

So far the watchdog code seems to have relied on the PERF_GLOBAL_CTRL enable bits being set by default, but at least on some Intel Sapphire and Emerald Rapids this is no longer the case, and Xen reports:

Testing NMI watchdog on all CPUs: 0 40 stuck

The first CPU on each package is started with PERF_GLOBAL_CTRL zeroed, so PMC0 doesn't start counting when the enable bit in EVNTSEL0 is set, due to the relevant enable bit in PERF_GLOBAL_CTRL not being set.

Check and adjust PERF_GLOBAL_CTRL during CPU initialization so that all the general-purpose PMCs are enabled. Doing so brings the state of the package-BSP PERF_GLOBAL_CTRL in line with the rest of the CPUs on the system.

---
Changes since v1:
- Do the adjustment of PERF_GLOBAL_CTRL even if the watchdog is not used, and enable all counters.
---
Unsure whether printing a warning if the value of PERF_GLOBAL_CTRL is not correct is of any value, hence I haven't added it.

---
 xen/arch/x86/cpu/intel.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
Quoted 12 lines

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 536

Quoted 9 lines
xen/arch/x86/cpu/intel.c +1 -0
index dfee64689ffe..40d3eb0e18a7 100644
@@ -533,9 +533,25 @@ static void cf_check init_intel(struct cpuinfo_x86 *c)
 	init_intel_cacheinfo(c);
 	if (c->cpuid_level > 9) {
 		unsigned eax = cpuid_eax(10);
+		unsigned int cnt = (uint8_t)(eax >> 8);

(Not just) since ./CODING_STYLE wants us to avoid fixed width types where possible, personally I'd prefer "& 0xff" here.

Quoted 2 lines

Hm, OK. I got confused and somehow was under the impression we prefer to truncate using fixed types rather than masks.

It's on the edge I admit, and iirc e.g. Andrew would rather see ./CODING_STYLE be relaxed some again.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/cpu/intel.c +8 -1
 		/* Check for version and the number of counters */
-		if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
+		if ((eax & 0xff) && (cnt > 1) && (cnt <= 32)) {
+			uint64_t global_ctrl;
+			unsigned int cnt_mask = (1UL << cnt) - 1;
+
+			/*
+			 * On (some?) Sapphire/Emerald Rapids platforms each
+			 * package-BSP starts with all the enable bits for the
+			 * general-purpose PMCs cleared.  Adjust so counters
Quoted 8 lines

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 552

Quoted 6 lines
xen/arch/x86/cpu/intel.c +6 -0
+			 * can be enabled from EVNTSEL.
+			 */
+			rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl);
+			if ((global_ctrl & cnt_mask) != cnt_mask)
+				wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
+				       global_ctrl | cnt_mask);

Should there perhaps be a log message?

Quoted 1 line

I had a post-commit remark about that exactly. I can add one.

Oh, sorry, missed that. Imo for firmware bugs it's useful to record. Further, if such a messages is emitted first, in the (hopefully) unlikely event of the WRMSR causing an issue, there'll be an immediate reference of what's going on.

Jan

#4 lore raw
In reply to Roger Pau Monne
Quoted 35 lines · Roger Pau Monne

When Architectural Performance Monitoring is available, the PERF_GLOBAL_CTRL MSR contains per-counter enable bits that is ANDed with the enable bit in the counter EVNTSEL MSR in order for a PMC counter to be enabled.

So far the watchdog code seems to have relied on the PERF_GLOBAL_CTRL enable bits being set by default, but at least on some Intel Sapphire and Emerald Rapids this is no longer the case, and Xen reports:

Testing NMI watchdog on all CPUs: 0 40 stuck

The first CPU on each package is started with PERF_GLOBAL_CTRL zeroed, so PMC0 doesn't start counting when the enable bit in EVNTSEL0 is set, due to the relevant enable bit in PERF_GLOBAL_CTRL not being set.

Check and adjust PERF_GLOBAL_CTRL during CPU initialization so that all the general-purpose PMCs are enabled. Doing so brings the state of the package-BSP PERF_GLOBAL_CTRL in line with the rest of the CPUs on the system.

---
Changes since v1:
- Do the adjustment of PERF_GLOBAL_CTRL even if the watchdog is not used, and enable all counters.
---
Unsure whether printing a warning if the value of PERF_GLOBAL_CTRL is not correct is of any value, hence I haven't added it.

---
 xen/arch/x86/cpu/intel.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
xen/arch/x86/cpu/intel.c +0 -0
index dfee64689ffe..40d3eb0e18a7 100644

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 539

Quoted 8 lines
xen/arch/x86/cpu/intel.c +3 -1
@@ -533,9 +533,25 @@ static void cf_check init_intel(struct cpuinfo_x86 *c)
 	init_intel_cacheinfo(c);
 	if (c->cpuid_level > 9) {
 		unsigned eax = cpuid_eax(10);
+		unsigned int cnt = (uint8_t)(eax >> 8);
+
 		/* Check for version and the number of counters */
-		if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
+		if ((eax & 0xff) && (cnt > 1) && (cnt <= 32)) {

I may not have looked closely enough, but I didn't find the limit of 32 being stated anywhere.

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 541

Quoted 2 lines
xen/arch/x86/cpu/intel.c +2 -0
+			uint64_t global_ctrl;
+			unsigned int cnt_mask = (1UL << cnt) - 1;

Bits 2 + 4 * n have an additional qualification as per SDM vol 4.

Jan

Quoted 15 lines
xen/arch/x86/cpu/intel.c +11 -0
+			/*
+			 * On (some?) Sapphire/Emerald Rapids platforms each
+			 * package-BSP starts with all the enable bits for the
+			 * general-purpose PMCs cleared.  Adjust so counters
+			 * can be enabled from EVNTSEL.
+			 */
+			rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl);
+			if ((global_ctrl & cnt_mask) != cnt_mask)
+				wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
+				       global_ctrl | cnt_mask);
 			__set_bit(X86_FEATURE_ARCH_PERFMON, c->x86_capability);
+		}
 	}
 
 	if ( !cpu_has(c, X86_FEATURE_XTOPOLOGY) )
#5 lore raw
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Roger Pau Monne

When Architectural Performance Monitoring is available, the PERF_GLOBAL_CTRL MSR contains per-counter enable bits that is ANDed with the enable bit in the counter EVNTSEL MSR in order for a PMC counter to be enabled.

So far the watchdog code seems to have relied on the PERF_GLOBAL_CTRL enable bits being set by default, but at least on some Intel Sapphire and Emerald Rapids this is no longer the case, and Xen reports:

Testing NMI watchdog on all CPUs: 0 40 stuck

The first CPU on each package is started with PERF_GLOBAL_CTRL zeroed, so PMC0 doesn't start counting when the enable bit in EVNTSEL0 is set, due to the relevant enable bit in PERF_GLOBAL_CTRL not being set.

Check and adjust PERF_GLOBAL_CTRL during CPU initialization so that all the general-purpose PMCs are enabled. Doing so brings the state of the package-BSP PERF_GLOBAL_CTRL in line with the rest of the CPUs on the system.

---
Changes since v1:
- Do the adjustment of PERF_GLOBAL_CTRL even if the watchdog is not used, and enable all counters.
---
Unsure whether printing a warning if the value of PERF_GLOBAL_CTRL is not correct is of any value, hence I haven't added it.

---
 xen/arch/x86/cpu/intel.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
Quoted 16 lines

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 539

Quoted 13 lines
xen/arch/x86/cpu/intel.c +3 -1
index dfee64689ffe..40d3eb0e18a7 100644
@@ -533,9 +533,25 @@ static void cf_check init_intel(struct cpuinfo_x86 *c)
 	init_intel_cacheinfo(c);
 	if (c->cpuid_level > 9) {
 		unsigned eax = cpuid_eax(10);
+		unsigned int cnt = (uint8_t)(eax >> 8);
+
 		/* Check for version and the number of counters */
-		if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
+		if ((eax & 0xff) && (cnt > 1) && (cnt <= 32)) {

I may not have looked closely enough, but I didn't find the limit of 32 being stated anywhere.

Hm, my copy of the SDM vol4 states that bits 31:n are the enable bits, where n is CPUID.0AH: EAX[15:8]. Bits 32, 33 and 34 control the Fixed PMCs.

Quoted 4 lines

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 541

Quoted 2 lines
xen/arch/x86/cpu/intel.c +2 -0
+			uint64_t global_ctrl;
+			unsigned int cnt_mask = (1UL << cnt) - 1;

Bits 2 + 4 * n have an additional qualification as per SDM vol 4.

Let me update my copy...

Looking at the version from December 2023, I see:

0 EN_PMC0 If CPUID.0AH: EAX[15:8] > 0
1 EN_PMC1 If CPUID.0AH: EAX[15:8] > 1
2 EN_PMC2 If CPUID.0AH: EAX[15:8] > 2
n EN_PMCn If CPUID.0AH: EAX[15:8] > n
31:n+1 Reserved.

And I'm afraid I'm not able to infer this additional qualification of bits 2 + 4 * n.

Thanks, Roger.

#6 lore raw
Quoted 62 lines · Roger Pau Monné
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Roger Pau Monne

When Architectural Performance Monitoring is available, the PERF_GLOBAL_CTRL MSR contains per-counter enable bits that is ANDed with the enable bit in the counter EVNTSEL MSR in order for a PMC counter to be enabled.

So far the watchdog code seems to have relied on the PERF_GLOBAL_CTRL enable bits being set by default, but at least on some Intel Sapphire and Emerald Rapids this is no longer the case, and Xen reports:

Testing NMI watchdog on all CPUs: 0 40 stuck

The first CPU on each package is started with PERF_GLOBAL_CTRL zeroed, so PMC0 doesn't start counting when the enable bit in EVNTSEL0 is set, due to the relevant enable bit in PERF_GLOBAL_CTRL not being set.

Check and adjust PERF_GLOBAL_CTRL during CPU initialization so that all the general-purpose PMCs are enabled. Doing so brings the state of the package-BSP PERF_GLOBAL_CTRL in line with the rest of the CPUs on the system.

---
Changes since v1:
- Do the adjustment of PERF_GLOBAL_CTRL even if the watchdog is not used, and enable all counters.
---
Unsure whether printing a warning if the value of PERF_GLOBAL_CTRL is not correct is of any value, hence I haven't added it.

---
 xen/arch/x86/cpu/intel.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
Quoted 16 lines

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 539

Quoted 13 lines
xen/arch/x86/cpu/intel.c +3 -1
index dfee64689ffe..40d3eb0e18a7 100644
@@ -533,9 +533,25 @@ static void cf_check init_intel(struct cpuinfo_x86 *c)
 	init_intel_cacheinfo(c);
 	if (c->cpuid_level > 9) {
 		unsigned eax = cpuid_eax(10);
+		unsigned int cnt = (uint8_t)(eax >> 8);
+
 		/* Check for version and the number of counters */
-		if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
+		if ((eax & 0xff) && (cnt > 1) && (cnt <= 32)) {

I may not have looked closely enough, but I didn't find the limit of 32 being stated anywhere.

Hm, my copy of the SDM vol4 states that bits 31:n are the enable bits, where n is CPUID.0AH: EAX[15:8]. Bits 32, 33 and 34 control the Fixed PMCs.

Quoted 4 lines

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 541

Quoted 2 lines
xen/arch/x86/cpu/intel.c +2 -0
+			uint64_t global_ctrl;
+			unsigned int cnt_mask = (1UL << cnt) - 1;

Bits 2 + 4 * n have an additional qualification as per SDM vol 4.

Let me update my copy...

Looking at the version from December 2023, I see:

Quoted 8 lines

0 EN_PMC0 If CPUID.0AH: EAX[15:8] > 0
1 EN_PMC1 If CPUID.0AH: EAX[15:8] > 1
2 EN_PMC2 If CPUID.0AH: EAX[15:8] > 2
n EN_PMCn If CPUID.0AH: EAX[15:8] > n
31:n+1 Reserved.

And I'm afraid I'm not able to infer this additional qualification of bits 2 + 4 * n.

I'm sorry, both earlier questions were my fault, due to looking at the table entry for the first match of PERF_GLOBAL_CTRL (i.e. IA32_FIXED_CTR_CTRL). Still need to get used to the new table layout, it seems.

Looking at the correct entry raises a question on cnt > 1 though, as EN_PMC0 is defined for cnt > 0.

Jan

#7 lore raw
Quoted 72 lines · Jan Beulich
Quoted 62 lines · Roger Pau Monné
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Roger Pau Monne

When Architectural Performance Monitoring is available, the PERF_GLOBAL_CTRL MSR contains per-counter enable bits that is ANDed with the enable bit in the counter EVNTSEL MSR in order for a PMC counter to be enabled.

So far the watchdog code seems to have relied on the PERF_GLOBAL_CTRL enable bits being set by default, but at least on some Intel Sapphire and Emerald Rapids this is no longer the case, and Xen reports:

Testing NMI watchdog on all CPUs: 0 40 stuck

The first CPU on each package is started with PERF_GLOBAL_CTRL zeroed, so PMC0 doesn't start counting when the enable bit in EVNTSEL0 is set, due to the relevant enable bit in PERF_GLOBAL_CTRL not being set.

Check and adjust PERF_GLOBAL_CTRL during CPU initialization so that all the general-purpose PMCs are enabled. Doing so brings the state of the package-BSP PERF_GLOBAL_CTRL in line with the rest of the CPUs on the system.

---
Changes since v1:
- Do the adjustment of PERF_GLOBAL_CTRL even if the watchdog is not used, and enable all counters.
---
Unsure whether printing a warning if the value of PERF_GLOBAL_CTRL is not correct is of any value, hence I haven't added it.

---
 xen/arch/x86/cpu/intel.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
Quoted 16 lines

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 539

Quoted 13 lines
xen/arch/x86/cpu/intel.c +3 -1
index dfee64689ffe..40d3eb0e18a7 100644
@@ -533,9 +533,25 @@ static void cf_check init_intel(struct cpuinfo_x86 *c)
 	init_intel_cacheinfo(c);
 	if (c->cpuid_level > 9) {
 		unsigned eax = cpuid_eax(10);
+		unsigned int cnt = (uint8_t)(eax >> 8);
+
 		/* Check for version and the number of counters */
-		if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
+		if ((eax & 0xff) && (cnt > 1) && (cnt <= 32)) {

I may not have looked closely enough, but I didn't find the limit of 32 being stated anywhere.

Hm, my copy of the SDM vol4 states that bits 31:n are the enable bits, where n is CPUID.0AH: EAX[15:8]. Bits 32, 33 and 34 control the Fixed PMCs.

Quoted 4 lines

xen/arch/x86/cpu/intel.c static void cf_check init_intel(struct cpuinfo_x86 *c) · line 541

Quoted 2 lines
xen/arch/x86/cpu/intel.c +2 -0
+			uint64_t global_ctrl;
+			unsigned int cnt_mask = (1UL << cnt) - 1;

Bits 2 + 4 * n have an additional qualification as per SDM vol 4.

Let me update my copy...

Looking at the version from December 2023, I see:

Quoted 8 lines

0 EN_PMC0 If CPUID.0AH: EAX[15:8] > 0
1 EN_PMC1 If CPUID.0AH: EAX[15:8] > 1
2 EN_PMC2 If CPUID.0AH: EAX[15:8] > 2
n EN_PMCn If CPUID.0AH: EAX[15:8] > n
31:n+1 Reserved.

And I'm afraid I'm not able to infer this additional qualification of bits 2 + 4 * n.

Quoted 7 lines

I'm sorry, both earlier questions were my fault, due to looking at the table entry for the first match of PERF_GLOBAL_CTRL (i.e. IA32_FIXED_CTR_CTRL). Still need to get used to the new table layout, it seems.

Looking at the correct entry raises a question on cnt > 1 though, as EN_PMC0 is defined for cnt > 0.

Oh, indeed, can adjust on this same patch if that's OK (seeing as the issue was already there previous to my change).

Thanks, Roger.

#8 lore raw
Quoted 2 lines · Roger Pau Monné

Oh, indeed, can adjust on this same patch if that's OK (seeing as the issue was already there previous to my change).

Well, I'm getting the impression that it was deliberate there, i.e. set setting of the feature flag may want to remain thus constrained.

Jan

#9 lore raw
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monné

Oh, indeed, can adjust on this same patch if that's OK (seeing as the issue was already there previous to my change).

Well, I'm getting the impression that it was deliberate there, i.e. set setting of the feature flag may want to remain thus constrained.

Hm, I find it weird, but the original commit message doesn't help at all. Xen itself only uses PMC0, and I don't find any other justification in the current code to require at least 2 counters in order to expose arch performance monitoring to be present.

Looking at the SDM vol3, the figures there only contain PMC0 and PMC1, so someone only reading that manual might assume there must always be 2 global PMCs?

(vol4 clarifies the that the number of global PMCs is variable).

Thanks, Roger.

#10 lore raw
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monné

Oh, indeed, can adjust on this same patch if that's OK (seeing as the issue was already there previous to my change).

Well, I'm getting the impression that it was deliberate there, i.e. set setting of the feature flag may want to remain thus constrained.

Quoted 8 lines

Hm, I find it weird, but the original commit message doesn't help at all. Xen itself only uses PMC0, and I don't find any other justification in the current code to require at least 2 counters in order to expose arch performance monitoring to be present.

Looking at the SDM vol3, the figures there only contain PMC0 and PMC1, so someone only reading that manual might assume there must always be 2 global PMCs?

That may have been the impression at the time. It may have been wrong already back then, or ...

Quoted 1 line

(vol4 clarifies the that the number of global PMCs is variable).

... it may have been clarified in the SDM later on. My vague guess is that the > 1 check was to skip what may have been "obviously buggy" back at the time.

Jan

#11 lore raw
Quoted 18 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monné

Oh, indeed, can adjust on this same patch if that's OK (seeing as the issue was already there previous to my change).

Well, I'm getting the impression that it was deliberate there, i.e. set setting of the feature flag may want to remain thus constrained.

Quoted 8 lines

Hm, I find it weird, but the original commit message doesn't help at all. Xen itself only uses PMC0, and I don't find any other justification in the current code to require at least 2 counters in order to expose arch performance monitoring to be present.

Looking at the SDM vol3, the figures there only contain PMC0 and PMC1, so someone only reading that manual might assume there must always be 2 global PMCs?

Quoted 8 lines

That may have been the impression at the time. It may have been wrong already back then, or ...

Quoted 1 line

(vol4 clarifies the that the number of global PMCs is variable).

... it may have been clarified in the SDM later on. My vague guess is that the > 1 check was to skip what may have been "obviously buggy" back at the time.

Let me know if you are OK with the adjustment in v3, or whether you would rather leave the > 1 check as-is (or maybe adjust in a different patch).

Thanks, Roger.

#12 lore raw
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monné

Oh, indeed, can adjust on this same patch if that's OK (seeing as the issue was already there previous to my change).

Well, I'm getting the impression that it was deliberate there, i.e. set setting of the feature flag may want to remain thus constrained.

Quoted 8 lines

Hm, I find it weird, but the original commit message doesn't help at all. Xen itself only uses PMC0, and I don't find any other justification in the current code to require at least 2 counters in order to expose arch performance monitoring to be present.

Looking at the SDM vol3, the figures there only contain PMC0 and PMC1, so someone only reading that manual might assume there must always be 2 global PMCs?

That may have been the impression at the time. It may have been wrong already back then, or ...

Quoted 1 line

(vol4 clarifies the that the number of global PMCs is variable).

Quoted 7 lines
Quoted 3 lines

... it may have been clarified in the SDM later on. My vague guess is that the > 1 check was to skip what may have been "obviously buggy" back at the time.

Let me know if you are OK with the adjustment in v3, or whether you would rather leave the > 1 check as-is (or maybe adjust in a different patch).

Well, I haven't been able to make up my mind as to whether the original check was wrong. Without clear indication, I think we should retain the original behavior by having the __set_bit() gated by an additional if(). Then, since the line needs touching anyway, a further question would be whether to properly switch to setup_force_cpu_cap() at the same time.

Jan

#13 lore raw
Quoted 33 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monné

Oh, indeed, can adjust on this same patch if that's OK (seeing as the issue was already there previous to my change).

Well, I'm getting the impression that it was deliberate there, i.e. set setting of the feature flag may want to remain thus constrained.

Quoted 8 lines

Hm, I find it weird, but the original commit message doesn't help at all. Xen itself only uses PMC0, and I don't find any other justification in the current code to require at least 2 counters in order to expose arch performance monitoring to be present.

Looking at the SDM vol3, the figures there only contain PMC0 and PMC1, so someone only reading that manual might assume there must always be 2 global PMCs?

That may have been the impression at the time. It may have been wrong already back then, or ...

Quoted 1 line

(vol4 clarifies the that the number of global PMCs is variable).

Quoted 7 lines
Quoted 3 lines

... it may have been clarified in the SDM later on. My vague guess is that the > 1 check was to skip what may have been "obviously buggy" back at the time.

Let me know if you are OK with the adjustment in v3, or whether you would rather leave the > 1 check as-is (or maybe adjust in a different patch).

Quoted 5 lines

Well, I haven't been able to make up my mind as to whether the original check was wrong. Without clear indication, I think we should retain the original behavior by having the __set_bit() gated by an additional if(). Then, since the line needs touching anyway, a further question would be whether to properly switch to setup_force_cpu_cap() at the same time.

Having looked at Linux, it has exactly the same check for > 1, which I guess is to be expected since the code in Xen is quite likely adapted from the code in Linux.

Overall, it might be best to leave the check as > 1. It's possible (as I think you also mention in a previous email) that there's simply no hardware with 1 counter. This might no longer be true when virtualized, but given the current checks in both Xen and Linux any virtualization environment that attempts to expose arch perf support would need to expose at least 2 PMCs.

My suggestion is to leave the cnt > 1 check as it is in v2.

I can send a v4 with that check fixed if there's nothing else in v3 that needs fixing.

IMO doing the adjustment to PERF_GLOBAL_CTRL without setting ARCH_PERFMON would be contradictory. Either we set ARCH_PERFMON support and consequently adjust PERF_GLOBAL_CTRL, or we don't.

Thanks, Roger.

#14 lore raw
Quoted 51 lines · Roger Pau Monné
Quoted 33 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monné

Oh, indeed, can adjust on this same patch if that's OK (seeing as the issue was already there previous to my change).

Well, I'm getting the impression that it was deliberate there, i.e. set setting of the feature flag may want to remain thus constrained.

Quoted 8 lines

Hm, I find it weird, but the original commit message doesn't help at all. Xen itself only uses PMC0, and I don't find any other justification in the current code to require at least 2 counters in order to expose arch performance monitoring to be present.

Looking at the SDM vol3, the figures there only contain PMC0 and PMC1, so someone only reading that manual might assume there must always be 2 global PMCs?

That may have been the impression at the time. It may have been wrong already back then, or ...

Quoted 1 line

(vol4 clarifies the that the number of global PMCs is variable).

Quoted 7 lines
Quoted 3 lines

... it may have been clarified in the SDM later on. My vague guess is that the > 1 check was to skip what may have been "obviously buggy" back at the time.

Let me know if you are OK with the adjustment in v3, or whether you would rather leave the > 1 check as-is (or maybe adjust in a different patch).

Quoted 5 lines

Well, I haven't been able to make up my mind as to whether the original check was wrong. Without clear indication, I think we should retain the original behavior by having the __set_bit() gated by an additional if(). Then, since the line needs touching anyway, a further question would be whether to properly switch to setup_force_cpu_cap() at the same time.

Having looked at Linux, it has exactly the same check for > 1, which I guess is to be expected since the code in Xen is quite likely adapted from the code in Linux.

Overall, it might be best to leave the check as > 1. It's possible (as I think you also mention in a previous email) that there's simply no hardware with 1 counter. This might no longer be true when virtualized, but given the current checks in both Xen and Linux any virtualization environment that attempts to expose arch perf support would need to expose at least 2 PMCs.

Quoted 8 lines

My suggestion is to leave the cnt > 1 check as it is in v2.

I can send a v4 with that check fixed if there's nothing else in v3 that needs fixing.

IMO doing the adjustment to PERF_GLOBAL_CTRL without setting ARCH_PERFMON would be contradictory. Either we set ARCH_PERFMON support and consequently adjust PERF_GLOBAL_CTRL, or we don't.

Probably fair enough.

Jan