Skip to contentSkip to conversation

perf pmu intel: Generalize SNC cpumask adjustment for multiple platforms

PATCH Superseded Chun-Tse Shao 3 participants 8 messages May 15 – May 27 perf events

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/2 perf pmu intel: Generalize SNC cpumask adjustment for multiple platforms

Prepare for supporting more Intel platforms with sub-NUMA clustering by generalizing the GNR specific logic.

---
v6:
Make string literal.
Add SPR into SNC2.

v5: lore.kernel.org/[email protected]/
Split patch.

v4: lore.kernel.org/[email protected]
Rebase.

v3: lore.kernel.org/[email protected]
Fix a typo.

v2: lore.kernel.org/[email protected]
Split EMR and GNR in the SNC2 IMC cpu map.

v1: lore.kernel.org/[email protected]

 tools/perf/arch/x86/util/pmu.c | 46 +++++++++++++++++++++-------------
 1 file changed, 29 insertions(+), 17 deletions(-)
tools/perf/arch/x86/util/pmu.c +29 -17
index 7c9d238922a6..b8fab260c423 100644
@@ -22,20 +22,30 @@
 #include "util/env.h"
 #include "util/header.h"
 
-static bool x86__is_intel_graniterapids(void)
+#define GENUINE_INTEL_GNR "GenuineIntel-6-A[DE]"
+
+static bool x86__is_snc_supported(void)
 {
-	static bool checked_if_graniterapids;
-	static bool is_graniterapids;
+	static bool checked_if_snc_supported;
+	static bool is_supported;
+
+	if (!checked_if_snc_supported) {
 
-	if (!checked_if_graniterapids) {
-		const char *graniterapids_cpuid = "GenuineIntel-6-A[DE]";
+		/* Graniterapids supports SNC configuration. */
+		static const char *const supported_cpuids[] = {
+			GENUINE_INTEL_GNR, /* Graniterapids */
+		};
 		char *cpuid = get_cpuid_str((struct perf_cpu){0});
 
-		is_graniterapids = cpuid && strcmp_cpuid_str(graniterapids_cpuid, cpuid) == 0;
+		for (size_t i = 0; i < ARRAY_SIZE(supported_cpuids); i++) {
+			is_supported = cpuid && strcmp_cpuid_str(supported_cpuids[i], cpuid) == 0;
+			if (is_supported)
+				break;
+		}
 		free(cpuid);
-		checked_if_graniterapids = true;
+		checked_if_snc_supported = true;
 	}
-	return is_graniterapids;
+	return is_supported;
 }
 
 static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
@@ -132,8 +142,8 @@ static int uncore_imc_snc(struct perf_pmu *pmu)
 	// Compute the IMC SNC using lookup tables.
 	unsigned int imc_num;
 	int snc_nodes = snc_nodes_per_l3_cache();
-	const u8 snc2_map[] = {1, 1, 0, 0, 1, 1, 0, 0};
-	const u8 snc3_map[] = {1, 1, 0, 0, 2, 2, 1, 1, 0, 0, 2, 2};
+	const u8 snc2_map[] = {1, 1, 0, 0};
+	const u8 snc3_map[] = {1, 1, 0, 0, 2, 2};
 	const u8 *snc_map;
 	size_t snc_map_len;
 
@@ -156,11 +166,12 @@ static int uncore_imc_snc(struct perf_pmu *pmu)
 		pr_warning("Unexpected: unable to compute IMC number '%s'\n", pmu->name);
 		return 0;
 	}
-	if (imc_num >= snc_map_len) {
+	if (imc_num >= snc_map_len * perf_cpu_map__nr(pmu->cpus)) {
 		pr_warning("Unexpected IMC %d for SNC%d mapping\n", imc_num, snc_nodes);
 		return 0;
 	}
-	return snc_map[imc_num];
+
+	return snc_map[imc_num % snc_map_len];
 }
 
 static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
@@ -200,7 +211,7 @@ static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
 	return cpu_adjust[pmu_snc];
 }
 
-static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
+static void uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
 {
 	// With sub-NUMA clustering (SNC) there is a NUMA node per SNC in the
 	// topology. For example, a two socket graniterapids machine may be set
@@ -300,11 +311,12 @@ void perf_pmu__arch_init(struct perf_pmu *pmu)
 				pmu->mem_events = perf_mem_events_intel_aux;
 			else
 				pmu->mem_events = perf_mem_events_intel;
-		} else if (x86__is_intel_graniterapids()) {
+		} else if (x86__is_snc_supported()) {
 			if (strstarts(pmu->name, "uncore_cha_"))
-				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
-			else if (strstarts(pmu->name, "uncore_imc_"))
-				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+				uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
+			else if (strstarts(pmu->name, "uncore_imc_") &&
+				 !strstarts(pmu->name, "uncore_imc_free_running"))
+				uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
 		}
 	}
 }

--
2.54.0.669.g59709faab0-goog

#1 lore raw

PATCH v6 2/2 perf pmu intel: Adjust cpumasks for sub-NUMA clusters on Sapphire Rapids and Emerald Rapids

Similar to GNR [1], Sapphire Rapids and Emerald Rapids support sub-NUMA clusters as well. Adjust cpumasks using the same logic as GNR in [1].

Tested on Emerald Rapids with SNC2 enabled:

  $ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a -- sleep 1

Performance counter stats for 'system wide':

  N0       30        72125876670      UNC_CHA_CLOCKTICKS
  N0        4         8815163648      UNC_M_CLOCKTICKS
  N1       30        72124958844      UNC_CHA_CLOCKTICKS
  N1        4         8815014974      UNC_M_CLOCKTICKS
  N2       30        72121049022      UNC_CHA_CLOCKTICKS
  N2        4         8814592626      UNC_M_CLOCKTICKS
  N3       30        72117133854      UNC_CHA_CLOCKTICKS
  N3        4         8814012840      UNC_M_CLOCKTICKS
         1.001574118 seconds time elapsed

[1] lore.kernel.org/[email protected]

---
 tools/perf/arch/x86/util/pmu.c | 60 ++++++++++++++++++++++++----------
 1 file changed, 42 insertions(+), 18 deletions(-)
tools/perf/arch/x86/util/pmu.c +42 -18
index b8fab260c423..09caec0a32a5 100644
@@ -22,6 +22,8 @@
 #include "util/env.h"
 #include "util/header.h"
 
+#define GENUINE_INTEL_SPR "GenuineIntel-6-8F"
+#define GENUINE_INTEL_EMR "GenuineIntel-6-CF"
 #define GENUINE_INTEL_GNR "GenuineIntel-6-A[DE]"
 
 static bool x86__is_snc_supported(void)
@@ -31,8 +33,10 @@ static bool x86__is_snc_supported(void)
 
 	if (!checked_if_snc_supported) {
 
-		/* Graniterapids supports SNC configuration. */
+		/* Sapphirerapids Emeraldrapids Graniterapids support SNC configuration. */
 		static const char *const supported_cpuids[] = {
+			GENUINE_INTEL_SPR, /* Sapphirerapids */
+			GENUINE_INTEL_EMR, /* Emeraldrapids */
 			GENUINE_INTEL_GNR, /* Graniterapids */
 		};
 		char *cpuid = get_cpuid_str((struct perf_cpu){0});
@@ -142,23 +146,43 @@ static int uncore_imc_snc(struct perf_pmu *pmu)
 	// Compute the IMC SNC using lookup tables.
 	unsigned int imc_num;
 	int snc_nodes = snc_nodes_per_l3_cache();
-	const u8 snc2_map[] = {1, 1, 0, 0};
-	const u8 snc3_map[] = {1, 1, 0, 0, 2, 2};
-	const u8 *snc_map;
-	size_t snc_map_len;
-
-	switch (snc_nodes) {
-	case 2:
-		snc_map = snc2_map;
-		snc_map_len = ARRAY_SIZE(snc2_map);
-		break;
-	case 3:
-		snc_map = snc3_map;
-		snc_map_len = ARRAY_SIZE(snc3_map);
-		break;
-	default:
-		/* Error or no lookup support for SNC with >3 nodes. */
-		return 0;
+	char *cpuid;
+	static const u8 spr_emr_snc2_map[] = { 0, 0, 1, 1 };
+	static const u8 gnr_snc2_map[] = { 1, 1, 0, 0 };
+	static const u8 snc3_map[] = { 1, 1, 0, 0, 2, 2 };
+	static const u8 *snc_map;
+	static size_t snc_map_len;
+
+	/* snc_map is not inited yet. We only look up once to avoid expensive operations. */
+	if (!snc_map) {
+		switch (snc_nodes) {
+		case 2:
+			cpuid = get_cpuid_str((struct perf_cpu){ 0 });
+			if (cpuid) {
+				if (strcmp_cpuid_str(GENUINE_INTEL_SPR, cpuid) == 0 ||
+				    strcmp_cpuid_str(GENUINE_INTEL_EMR, cpuid) == 0) {
+					snc_map = spr_emr_snc2_map;
+					snc_map_len = ARRAY_SIZE(spr_emr_snc2_map);
+				} else if (strcmp_cpuid_str(GENUINE_INTEL_GNR, cpuid) == 0) {
+					snc_map = gnr_snc2_map;
+					snc_map_len = ARRAY_SIZE(gnr_snc2_map);
+				}
+				free(cpuid);
+			}
+			break;
+		case 3:
+			snc_map = snc3_map;
+			snc_map_len = ARRAY_SIZE(snc3_map);
+			break;
+		default:
+			/* Error or no lookup support for SNC with >3 nodes. */
+			return 0;
+		}
+
+		if (!snc_map) {
+			pr_warning("Unexpected: can not find snc map config");
+			return 0;
+		}
 	}
 
 	/* Compute SNC for PMU. */

--
2.54.0.669.g59709faab0-goog

#2 lore raw

Re: [PATCH v6 2/2] perf pmu intel: Adjust cpumasks for sub-NUMA clusters on Sapphire Rapids and Emerald Rapids

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Missing IMC cpumask adjustment and silent failure for SNC4 configurations on Sapphire Rapids. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missing IMC cpumask adjustment and silent failure for SNC4 configurations on Sapphire Rapids. --

commit 312ea8a1894ae6a4d9318072afccaf0abbbb29bd
Author: Chun-Tse Shao <[email protected]>

perf pmu intel: Adjust cpumasks for sub-NUMA clusters on Sapphire Rapids and Emerald Rapids

This commit adjusts the cpumasks for sub-NUMA clusters on Sapphire Rapids and Emerald Rapids processors. It updates the detection logic and the mapping arrays to support SNC configurations for these architectures.

Quoted 4 lines
tools/perf/arch/x86/util/pmu.c +0 -0
index b8fab260c4238..09caec0a32a57 100644

[ ... ]

Quoted 4 lines
tools/perf/arch/x86/util/pmu.c +0 -0
@@ -142,23 +146,43 @@ static int uncore_imc_snc(struct perf_pmu *pmu)
 	// Compute the IMC SNC using lookup tables.
 	unsigned int imc_num;
 	int snc_nodes = snc_nodes_per_l3_cache();

[ ... ]

Quoted 4 lines
tools/perf/arch/x86/util/pmu.c +4 -0
+	/* snc_map is not inited yet. We only look up once to avoid expensive operations. */
+	if (!snc_map) {
+		switch (snc_nodes) {
+		case 2:

[ ... ]

tools/perf/arch/x86/util/pmu.c static int uncore_imc_snc(struct perf_pmu *pmu) · line 179

Quoted 8 lines
tools/perf/arch/x86/util/pmu.c +8 -0
+			break;
+		case 3:
+			snc_map = snc3_map;
+			snc_map_len = ARRAY_SIZE(snc3_map);
+			break;
+		default:
+			/* Error or no lookup support for SNC with >3 nodes. */
+			return 0;

Sapphire Rapids natively supports 4-way sub-NUMA clustering, which would make snc_nodes evaluate to 4. Does this regression silently skip IMC cpumask adjustments for SNC4 configurations?

Because this default case returns 0 immediately, it entirely bypasses the newly added warning block below.

tools/perf/arch/x86/util/pmu.c static int uncore_imc_snc(struct perf_pmu *pmu) · line 185

Quoted 6 lines
tools/perf/arch/x86/util/pmu.c +6 -0
+		}
+
+		if (!snc_map) {
+			pr_warning("Unexpected: can not find snc map config");
+			return 0;
+		}

Could this leave users unaware that the IMC PMUs are unmapped, even though CHA PMUs are correctly adjusted for SNC4 elsewhere?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#3 lore raw
In reply to Chun-Tse Shao
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The bounds check for `imc_num` incorrectly limits the maximum IMC number based on the number of…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The bounds check for imc_num incorrectly limits the maximum IMC number based on the number of active sockets, causing valid IMC PMUs to be rejected and leaving their cpumasks unadjusted on single-socket systems.
- [High] A divide-by-zero crash can occur when computing cha_snc due to an unhandled edge case where the number of discovered CHA PMUs is less than the calculated snc_nodes.
--

commit c91c5bd96c1936822e95d0f574b8f633d5afb4c1
Author: Chun-Tse Shao <[email protected]>

perf pmu intel: Generalize SNC cpumask adjustment for multiple platforms

Prepare for supporting more Intel platforms with sub-NUMA clustering by generalizing the GNR specific logic.

Quoted 4 lines
tools/perf/arch/x86/util/pmu.c +0 -0
index 7c9d238922a6c..b8fab260c4238 100644

[ ... ]

tools/perf/arch/x86/util/pmu.c static int uncore_imc_snc(struct perf_pmu *pmu) · line 169

Quoted 6 lines
tools/perf/arch/x86/util/pmu.c +1 -1
@@ -156,11 +166,12 @@ static int uncore_imc_snc(struct perf_pmu *pmu)
 		pr_warning("Unexpected: unable to compute IMC number '%s'\n", pmu->name);
 		return 0;
 	}
-	if (imc_num >= snc_map_len) {
+	if (imc_num >= snc_map_len * perf_cpu_map__nr(pmu->cpus)) {

Will this condition incorrectly limit the maximum IMC number based on the number of active sockets?

The expression perf_cpu_map__nr(pmu->cpus) represents the number of active sockets, meaning it will evaluate to 1 on a single-socket system. In an SNC3 configuration where snc_map_len is 6, this would limit imc_num to less than 6.

Could this cause single-socket systems to wrongly reject imc_num >= 6, given that the hardware might still expose 12 IMC blocks on that socket? If so, processing uncore_imc_6 through uncore_imc_11 would trigger the "Unexpected IMC" warning and skip the necessary cpumask adjustments.

(Note that subsequent patches in the series, such as "perf pmu intel:
Adjust cpumasks for sub-NUMA clusters on Sapphire Rapids and Emerald Rapids", do not appear to resolve this bounds check).

Quoted 3 lines
tools/perf/arch/x86/util/pmu.c +0 -0
 		pr_warning("Unexpected IMC %d for SNC%d mapping\n", imc_num, snc_nodes);
 		return 0;
 	}

[ ... ]

tools/perf/arch/x86/util/pmu.c void perf_pmu__arch_init(struct perf_pmu *pmu) · line 316

Quoted 11 lines
tools/perf/arch/x86/util/pmu.c +2 -4
@@ -300,11 +311,12 @@ void perf_pmu__arch_init(struct perf_pmu *pmu)
 				pmu->mem_events = perf_mem_events_intel_aux;
 			else
 				pmu->mem_events = perf_mem_events_intel;
-		} else if (x86__is_intel_graniterapids()) {
+		} else if (x86__is_snc_supported()) {
 			if (strstarts(pmu->name, "uncore_cha_"))
-				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
-			else if (strstarts(pmu->name, "uncore_imc_"))
-				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+				uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);

Is it possible for a divide-by-zero to occur when this generalized adjustment logic invokes uncore_cha_snc()?

If a system or container restricts access to sysfs and exposes fewer CHA devices, or if testing under a fake PMU environment, the dynamically determined num_cha could be less than the calculated snc_nodes.

In uncore_cha_snc(), the code performs:

chas_per_node = num_cha / snc_nodes;

And later evaluates:

cha_snc = cha_num / chas_per_node;

Could the initial integer division result in chas_per_node = 0, causing an unhandled crash when evaluating cha_snc?

Quoted 6 lines
tools/perf/arch/x86/util/pmu.c +3 -0
+			else if (strstarts(pmu->name, "uncore_imc_") &&
+				 !strstarts(pmu->name, "uncore_imc_free_running"))
+				uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
 		}
 	}
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v6 1/2
#4 lore raw
In reply to Chun-Tse Shao
Quoted 5 lines · Chun-Tse Shao

Prepare for supporting more Intel platforms with sub-NUMA clustering by generalizing the GNR specific logic.

---

Quoted 120 lines

v6:
Make string literal.
Add SPR into SNC2.

v5: lore.kernel.org/[email protected]/
Split patch.

v4: lore.kernel.org/[email protected]
Rebase.

v3: lore.kernel.org/[email protected]
Fix a typo.

v2: lore.kernel.org/[email protected]
Split EMR and GNR in the SNC2 IMC cpu map.

v1: lore.kernel.org/[email protected]

 tools/perf/arch/x86/util/pmu.c | 46 +++++++++++++++++++++-------------
 1 file changed, 29 insertions(+), 17 deletions(-)
tools/perf/arch/x86/util/pmu.c +29 -18
index 7c9d238922a6..b8fab260c423 100644
@@ -22,20 +22,30 @@
 #include "util/env.h"
 #include "util/header.h"
 
-static bool x86__is_intel_graniterapids(void)
+#define GENUINE_INTEL_GNR "GenuineIntel-6-A[DE]"
+
+static bool x86__is_snc_supported(void)
 {
-	static bool checked_if_graniterapids;
-	static bool is_graniterapids;
+	static bool checked_if_snc_supported;
+	static bool is_supported;
+
+	if (!checked_if_snc_supported) {
 
-	if (!checked_if_graniterapids) {
-		const char *graniterapids_cpuid = "GenuineIntel-6-A[DE]";
+		/* Graniterapids supports SNC configuration. */
+		static const char *const supported_cpuids[] = {
+			GENUINE_INTEL_GNR, /* Graniterapids */
+		};
 		char *cpuid = get_cpuid_str((struct perf_cpu){0});
 
-		is_graniterapids = cpuid && strcmp_cpuid_str(graniterapids_cpuid, cpuid) == 0;
+		for (size_t i = 0; i < ARRAY_SIZE(supported_cpuids); i++) {
+			is_supported = cpuid && strcmp_cpuid_str(supported_cpuids[i], cpuid) == 0;
+			if (is_supported)
+				break;
+		}
 		free(cpuid);
-		checked_if_graniterapids = true;
+		checked_if_snc_supported = true;
 	}
-	return is_graniterapids;
+	return is_supported;
 }
 
 static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
@@ -132,8 +142,8 @@ static int uncore_imc_snc(struct perf_pmu *pmu)
 	// Compute the IMC SNC using lookup tables.
 	unsigned int imc_num;
 	int snc_nodes = snc_nodes_per_l3_cache();
-	const u8 snc2_map[] = {1, 1, 0, 0, 1, 1, 0, 0};
-	const u8 snc3_map[] = {1, 1, 0, 0, 2, 2, 1, 1, 0, 0, 2, 2};
+	const u8 snc2_map[] = {1, 1, 0, 0};
+	const u8 snc3_map[] = {1, 1, 0, 0, 2, 2};
 	const u8 *snc_map;
 	size_t snc_map_len;
 
@@ -156,11 +166,12 @@ static int uncore_imc_snc(struct perf_pmu *pmu)
 		pr_warning("Unexpected: unable to compute IMC number '%s'\n", pmu->name);
 		return 0;
 	}
-	if (imc_num >= snc_map_len) {
+	if (imc_num >= snc_map_len * perf_cpu_map__nr(pmu->cpus)) {
 		pr_warning("Unexpected IMC %d for SNC%d mapping\n", imc_num, snc_nodes);
 		return 0;
 	}
-	return snc_map[imc_num];
+
+	return snc_map[imc_num % snc_map_len];
 }
 
 static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
@@ -200,7 +211,7 @@ static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
 	return cpu_adjust[pmu_snc];
 }
 
-static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
+static void uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
 {
 	// With sub-NUMA clustering (SNC) there is a NUMA node per SNC in the
 	// topology. For example, a two socket graniterapids machine may be set
@@ -300,11 +311,12 @@ void perf_pmu__arch_init(struct perf_pmu *pmu)
 				pmu->mem_events = perf_mem_events_intel_aux;
 			else
 				pmu->mem_events = perf_mem_events_intel;
-		} else if (x86__is_intel_graniterapids()) {
+		} else if (x86__is_snc_supported()) {
 			if (strstarts(pmu->name, "uncore_cha_"))
-				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
-			else if (strstarts(pmu->name, "uncore_imc_"))
-				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+				uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
+			else if (strstarts(pmu->name, "uncore_imc_") &&
+				 !strstarts(pmu->name, "uncore_imc_free_running"))
+				uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
 		}
 	}
 }
--

2.54.0.669.g59709faab0-goog

#5 lore raw

PATCH v7 1/2 perf pmu intel: Generalize SNC cpumask adjustment for multiple platforms

In reply to Chun-Tse Shao

Prepare for supporting more Intel platforms with sub-NUMA clustering by generalizing the GNR specific logic.

Assisted-by Gemini:gemini-3.1-pro-preview

---
v7:
Fixed based on Sashiko review:
- Refactor PMU initialization to be fully thread-safe using pthread_once and pthread_mutex locks.
- Fix a critical bounds check bug on single-socket systems.
- Fix potential divide-by-zero crash in uncore_cha_snc.
- Avoid spurious warnings on non-SNC systems.
- Avoid silent and inconsistent uncore mappings on unsupported SNC configurations.
- Resolve checkpatch linter warnings.

v6: lore.kernel.org/[email protected]
Make string literal.
Add SPR into SNC2.

v5: lore.kernel.org/[email protected]
Split patch.

v4: lore.kernel.org/[email protected]
Rebase.

v3: lore.kernel.org/[email protected]
Fix a typo.

v2: lore.kernel.org/[email protected]
Split EMR and GNR in the SNC2 IMC cpu map.

v1: lore.kernel.org/[email protected]

 tools/perf/arch/x86/util/pmu.c | 199 +++++++++++++++++++++------------
 1 file changed, 130 insertions(+), 69 deletions(-)
tools/perf/arch/x86/util/pmu.c +130 -69
index 7c9d238922a6..9b00d5720fb7 100644
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <pthread.h>
 #include <string.h>
 #include <stdio.h>
 #include <sys/types.h>
@@ -22,20 +23,31 @@
 #include "util/env.h"
 #include "util/header.h"
 
-static bool x86__is_intel_graniterapids(void)
-{
-	static bool checked_if_graniterapids;
-	static bool is_graniterapids;
+#define GENUINE_INTEL_GNR "GenuineIntel-6-A[DE]"
 
-	if (!checked_if_graniterapids) {
-		const char *graniterapids_cpuid = "GenuineIntel-6-A[DE]";
-		char *cpuid = get_cpuid_str((struct perf_cpu){0});
+static bool cached_snc_supported;
+static pthread_once_t snc_support_once = PTHREAD_ONCE_INIT;
 
-		is_graniterapids = cpuid && strcmp_cpuid_str(graniterapids_cpuid, cpuid) == 0;
-		free(cpuid);
-		checked_if_graniterapids = true;
+static void init_snc_support(void)
+{
+	/* Graniterapids supports SNC configuration. */
+	static const char *const supported_cpuids[] = {
+		GENUINE_INTEL_GNR, /* Graniterapids */
+	};
+	char *cpuid = get_cpuid_str((struct perf_cpu){0});
+
+	for (size_t i = 0; i < ARRAY_SIZE(supported_cpuids); i++) {
+		cached_snc_supported = cpuid && strcmp_cpuid_str(supported_cpuids[i], cpuid) == 0;
+		if (cached_snc_supported)
+			break;
 	}
-	return is_graniterapids;
+	free(cpuid);
+}
+
+static bool x86__is_snc_supported(void)
+{
+	pthread_once(&snc_support_once, init_snc_support);
+	return cached_snc_supported;
 }
 
 static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
@@ -52,49 +64,58 @@ static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
 	return cpus;
 }
 
-static int snc_nodes_per_l3_cache(void)
+static int cached_snc_nodes;
+static pthread_once_t snc_nodes_once = PTHREAD_ONCE_INIT;
+
+static void init_snc_nodes(void)
 {
-	static bool checked_snc;
-	static int snc_nodes;
-
-	if (!checked_snc) {
-		struct perf_cpu_map *node_cpus =
-			read_sysfs_cpu_map("devices/system/node/node0/cpulist");
-		struct perf_cpu_map *cache_cpus =
-			read_sysfs_cpu_map("devices/system/cpu/cpu0/cache/index3/shared_cpu_list");
-
-		snc_nodes = perf_cpu_map__nr(cache_cpus) / perf_cpu_map__nr(node_cpus);
-		perf_cpu_map__put(cache_cpus);
-		perf_cpu_map__put(node_cpus);
-		checked_snc = true;
-	}
-	return snc_nodes;
+	struct perf_cpu_map *node_cpus =
+		read_sysfs_cpu_map("devices/system/node/node0/cpulist");
+	struct perf_cpu_map *cache_cpus =
+		read_sysfs_cpu_map("devices/system/cpu/cpu0/cache/index3/shared_cpu_list");
+
+	if (node_cpus && cache_cpus)
+		cached_snc_nodes = perf_cpu_map__nr(cache_cpus) / perf_cpu_map__nr(node_cpus);
+	else
+		cached_snc_nodes = 0;
+	perf_cpu_map__put(cache_cpus);
+	perf_cpu_map__put(node_cpus);
 }
 
-static int num_chas(void)
+static int snc_nodes_per_l3_cache(void)
 {
-	static bool checked_chas;
-	static int num_chas;
+	pthread_once(&snc_nodes_once, init_snc_nodes);
+	return cached_snc_nodes;
+}
 
-	if (!checked_chas) {
-		int fd = perf_pmu__event_source_devices_fd();
-		struct io_dir dir;
-		struct io_dirent64 *dent;
+static int cached_num_chas;
+static pthread_once_t num_chas_once = PTHREAD_ONCE_INIT;
 
-		if (fd < 0)
-			return -1;
+static void init_num_chas(void)
+{
+	int fd = perf_pmu__event_source_devices_fd();
+	struct io_dir dir;
+	struct io_dirent64 *dent;
 
-		io_dir__init(&dir, fd);
+	if (fd < 0) {
+		cached_num_chas = -1;
+		return;
+	}
 
-		while ((dent = io_dir__readdir(&dir)) != NULL) {
-			/* Note, dent->d_type will be DT_LNK and so isn't a useful filter. */
-			if (strstarts(dent->d_name, "uncore_cha_"))
-				num_chas++;
-		}
-		close(fd);
-		checked_chas = true;
+	io_dir__init(&dir, fd);
+
+	while ((dent = io_dir__readdir(&dir)) != NULL) {
+		/* Note, dent->d_type will be DT_LNK and so isn't a useful filter. */
+		if (strstarts(dent->d_name, "uncore_cha_"))
+			cached_num_chas++;
 	}
-	return num_chas;
+	close(fd);
+}
+
+static int num_chas(void)
+{
+	pthread_once(&num_chas_once, init_num_chas);
+	return cached_num_chas;
 }
 
 #define MAX_SNCS 6
@@ -121,46 +142,73 @@ static int uncore_cha_snc(struct perf_pmu *pmu)
 		return 0;
 	}
 	chas_per_node = num_cha / snc_nodes;
+	if (chas_per_node == 0) {
+		pr_warning("Unexpected: chas_per_node is 0 (num_cha=%d, snc_nodes=%d)\n",
+			   num_cha, snc_nodes);
+		return 0;
+	}
 	cha_snc = cha_num / chas_per_node;
 
 	/* Range check cha_snc. for unexpected out of bounds. */
 	return cha_snc >= MAX_SNCS ? 0 : cha_snc;
 }
 
-static int uncore_imc_snc(struct perf_pmu *pmu)
+static const u8 *cached_imc_snc_map;
+static size_t cached_imc_snc_map_len;
+static pthread_once_t imc_snc_map_once = PTHREAD_ONCE_INIT;
+
+static void init_snc_map(void)
 {
-	// Compute the IMC SNC using lookup tables.
-	unsigned int imc_num;
 	int snc_nodes = snc_nodes_per_l3_cache();
-	const u8 snc2_map[] = {1, 1, 0, 0, 1, 1, 0, 0};
-	const u8 snc3_map[] = {1, 1, 0, 0, 2, 2, 1, 1, 0, 0, 2, 2};
-	const u8 *snc_map;
-	size_t snc_map_len;
+	char *cpuid;
+	static const u8 gnr_snc2_map[] = { 1, 1, 0, 0 };
+	static const u8 snc3_map[] = { 1, 1, 0, 0, 2, 2 };
 
 	switch (snc_nodes) {
 	case 2:
-		snc_map = snc2_map;
-		snc_map_len = ARRAY_SIZE(snc2_map);
+		cpuid = get_cpuid_str((struct perf_cpu){ 0 });
+		if (cpuid) {
+			if (strcmp_cpuid_str(GENUINE_INTEL_GNR, cpuid) == 0) {
+				cached_imc_snc_map = gnr_snc2_map;
+				cached_imc_snc_map_len = ARRAY_SIZE(gnr_snc2_map);
+			}
+			free(cpuid);
+		}
 		break;
 	case 3:
-		snc_map = snc3_map;
-		snc_map_len = ARRAY_SIZE(snc3_map);
+		cached_imc_snc_map = snc3_map;
+		cached_imc_snc_map_len = ARRAY_SIZE(snc3_map);
 		break;
 	default:
 		/* Error or no lookup support for SNC with >3 nodes. */
-		return 0;
+		break;
 	}
 
+	if (!cached_imc_snc_map)
+		pr_warning("Unexpected: can not find snc map config\n");
+}
+
+static int uncore_imc_snc(struct perf_pmu *pmu)
+{
+	// Compute the IMC SNC using lookup tables.
+	unsigned int imc_num;
+	int snc_nodes = snc_nodes_per_l3_cache();
+
+	if (snc_nodes <= 1)
+		return 0;
+
+	pthread_once(&imc_snc_map_once, init_snc_map);
+
 	/* Compute SNC for PMU. */
 	if (sscanf(pmu->name, "uncore_imc_%u", &imc_num) != 1) {
 		pr_warning("Unexpected: unable to compute IMC number '%s'\n", pmu->name);
 		return 0;
 	}
-	if (imc_num >= snc_map_len) {
-		pr_warning("Unexpected IMC %d for SNC%d mapping\n", imc_num, snc_nodes);
+
+	if (!cached_imc_snc_map)
 		return 0;
-	}
-	return snc_map[imc_num];
+
+	return cached_imc_snc_map[imc_num % cached_imc_snc_map_len];
 }
 
 static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
@@ -200,7 +248,9 @@ static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
 	return cpu_adjust[pmu_snc];
 }
 
-static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
+static pthread_mutex_t pmu_adjust_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
 {
 	// With sub-NUMA clustering (SNC) there is a NUMA node per SNC in the
 	// topology. For example, a two socket graniterapids machine may be set
@@ -231,9 +281,11 @@ static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool
 		return;
 	}
 
+	pthread_mutex_lock(&pmu_adjust_mutex);
+
 	pmu_snc = cha ? uncore_cha_snc(pmu) : uncore_imc_snc(pmu);
 	if (pmu_snc == 0) {
-		// No adjustment necessary for the first SNC.
+		pthread_mutex_unlock(&pmu_adjust_mutex);
 		return;
 	}
 
@@ -242,8 +294,10 @@ static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool
 		// Hold onto the perf_cpu_map globally to avoid recomputation.
 		cpu_adjust = uncore_cha_imc_compute_cpu_adjust(pmu_snc);
 		adjusted[pmu_snc] = perf_cpu_map__empty_new(perf_cpu_map__nr(pmu->cpus));
-		if (!adjusted[pmu_snc])
+		if (!adjusted[pmu_snc]) {
+			pthread_mutex_unlock(&pmu_adjust_mutex);
 			return;
+		}
 	}
 
 	perf_cpu_map__for_each_cpu(cpu, idx, pmu->cpus) {
@@ -263,6 +317,8 @@ static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool
 
 	perf_cpu_map__put(pmu->cpus);
 	pmu->cpus = perf_cpu_map__get(adjusted[pmu_snc]);
+
+	pthread_mutex_unlock(&pmu_adjust_mutex);
 }
 
 void perf_pmu__arch_init(struct perf_pmu *pmu)
@@ -300,11 +356,16 @@ void perf_pmu__arch_init(struct perf_pmu *pmu)
 				pmu->mem_events = perf_mem_events_intel_aux;
 			else
 				pmu->mem_events = perf_mem_events_intel;
-		} else if (x86__is_intel_graniterapids()) {
-			if (strstarts(pmu->name, "uncore_cha_"))
-				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
-			else if (strstarts(pmu->name, "uncore_imc_"))
-				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+		} else if (x86__is_snc_supported()) {
+			int snc_nodes = snc_nodes_per_l3_cache();
+
+			if (snc_nodes == 2 || snc_nodes == 3) {
+				if (strstarts(pmu->name, "uncore_cha_"))
+					uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
+				else if (strstarts(pmu->name, "uncore_imc_") &&
+					 !strstarts(pmu->name, "uncore_imc_free_running"))
+					uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+			}
 		}
 	}
 }

--
2.54.0.823.g6e5bcc1fc9-goog

#6 lore raw

PATCH v7 2/2 perf pmu intel: Adjust cpumasks for sub-NUMA clusters on Sapphire Rapids and Emerald Rapids

Similar to GNR [1], Sapphire Rapids and Emerald Rapids support sub-NUMA clusters as well. Adjust cpumasks using the same logic as GNR in [1].

Tested on Emerald Rapids with SNC2 enabled:

  $ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a -- sleep 1

Performance counter stats for 'system wide':

  N0       30        72125876670      UNC_CHA_CLOCKTICKS
  N0        4         8815163648      UNC_M_CLOCKTICKS
  N1       30        72124958844      UNC_CHA_CLOCKTICKS
  N1        4         8815014974      UNC_M_CLOCKTICKS
  N2       30        72121049022      UNC_CHA_CLOCKTICKS
  N2        4         8814592626      UNC_M_CLOCKTICKS
  N3       30        72117133854      UNC_CHA_CLOCKTICKS
  N3        4         8814012840      UNC_M_CLOCKTICKS
         1.001574118 seconds time elapsed

[1] lore.kernel.org/[email protected]

Assisted-by Gemini:gemini-3.1-pro-preview
---
 tools/perf/arch/x86/util/pmu.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
tools/perf/arch/x86/util/pmu.c +11 -2
index 9b00d5720fb7..2c24ef3140da 100644
@@ -23,6 +23,8 @@
 #include "util/env.h"
 #include "util/header.h"
 
+#define GENUINE_INTEL_SPR "GenuineIntel-6-8F"
+#define GENUINE_INTEL_EMR "GenuineIntel-6-CF"
 #define GENUINE_INTEL_GNR "GenuineIntel-6-A[DE]"
 
 static bool cached_snc_supported;
@@ -30,8 +32,10 @@ static pthread_once_t snc_support_once = PTHREAD_ONCE_INIT;
 
 static void init_snc_support(void)
 {
-	/* Graniterapids supports SNC configuration. */
+	/* Sapphirerapids Emeraldrapids Graniterapids support SNC configuration. */
 	static const char *const supported_cpuids[] = {
+		GENUINE_INTEL_SPR, /* Sapphirerapids */
+		GENUINE_INTEL_EMR, /* Emeraldrapids */
 		GENUINE_INTEL_GNR, /* Graniterapids */
 	};
 	char *cpuid = get_cpuid_str((struct perf_cpu){0});
@@ -161,6 +165,7 @@ static void init_snc_map(void)
 {
 	int snc_nodes = snc_nodes_per_l3_cache();
 	char *cpuid;
+	static const u8 spr_emr_snc2_map[] = { 0, 0, 1, 1 };
 	static const u8 gnr_snc2_map[] = { 1, 1, 0, 0 };
 	static const u8 snc3_map[] = { 1, 1, 0, 0, 2, 2 };
 
@@ -168,7 +173,11 @@ static void init_snc_map(void)
 	case 2:
 		cpuid = get_cpuid_str((struct perf_cpu){ 0 });
 		if (cpuid) {
-			if (strcmp_cpuid_str(GENUINE_INTEL_GNR, cpuid) == 0) {
+			if (strcmp_cpuid_str(GENUINE_INTEL_SPR, cpuid) == 0 ||
+			    strcmp_cpuid_str(GENUINE_INTEL_EMR, cpuid) == 0) {
+				cached_imc_snc_map = spr_emr_snc2_map;
+				cached_imc_snc_map_len = ARRAY_SIZE(spr_emr_snc2_map);
+			} else if (strcmp_cpuid_str(GENUINE_INTEL_GNR, cpuid) == 0) {
 				cached_imc_snc_map = gnr_snc2_map;
 				cached_imc_snc_map_len = ARRAY_SIZE(gnr_snc2_map);
 			}

--
2.54.0.823.g6e5bcc1fc9-goog

#7 lore raw
In reply to Chun-Tse Shao

Accidentally in reply to the previous version of patchset, will resend. Sorry for the interruption.

Quoted 346 lines · Chun-Tse Shao

Prepare for supporting more Intel platforms with sub-NUMA clustering by generalizing the GNR specific logic.

Assisted-by Gemini:gemini-3.1-pro-preview

---
v7:
Fixed based on Sashiko review:
- Refactor PMU initialization to be fully thread-safe using pthread_once and pthread_mutex locks.
- Fix a critical bounds check bug on single-socket systems.
- Fix potential divide-by-zero crash in uncore_cha_snc.
- Avoid spurious warnings on non-SNC systems.
- Avoid silent and inconsistent uncore mappings on unsupported SNC configurations.
- Resolve checkpatch linter warnings.

v6: lore.kernel.org/[email protected]
Make string literal.
Add SPR into SNC2.

v5: lore.kernel.org/[email protected]
Split patch.

v4: lore.kernel.org/[email protected]
Rebase.

v3: lore.kernel.org/[email protected]
Fix a typo.

v2: lore.kernel.org/[email protected]
Split EMR and GNR in the SNC2 IMC cpu map.

v1: lore.kernel.org/[email protected]

 tools/perf/arch/x86/util/pmu.c | 199 +++++++++++++++++++++------------
 1 file changed, 130 insertions(+), 69 deletions(-)
tools/perf/arch/x86/util/pmu.c +130 -70
index 7c9d238922a6..9b00d5720fb7 100644
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <pthread.h>
 #include <string.h>
 #include <stdio.h>
 #include <sys/types.h>
@@ -22,20 +23,31 @@
 #include "util/env.h"
 #include "util/header.h"
 
-static bool x86__is_intel_graniterapids(void)
-{
-       static bool checked_if_graniterapids;
-       static bool is_graniterapids;
+#define GENUINE_INTEL_GNR "GenuineIntel-6-A[DE]"
 
-       if (!checked_if_graniterapids) {
-               const char *graniterapids_cpuid = "GenuineIntel-6-A[DE]";
-               char *cpuid = get_cpuid_str((struct perf_cpu){0});
+static bool cached_snc_supported;
+static pthread_once_t snc_support_once = PTHREAD_ONCE_INIT;
 
-               is_graniterapids = cpuid && strcmp_cpuid_str(graniterapids_cpuid, cpuid) == 0;
-               free(cpuid);
-               checked_if_graniterapids = true;
+static void init_snc_support(void)
+{
+       /* Graniterapids supports SNC configuration. */
+       static const char *const supported_cpuids[] = {
+               GENUINE_INTEL_GNR, /* Graniterapids */
+       };
+       char *cpuid = get_cpuid_str((struct perf_cpu){0});
+
+       for (size_t i = 0; i < ARRAY_SIZE(supported_cpuids); i++) {
+               cached_snc_supported = cpuid && strcmp_cpuid_str(supported_cpuids[i], cpuid) == 0;
+               if (cached_snc_supported)
+                       break;
        }
-       return is_graniterapids;
+       free(cpuid);
+}
+
+static bool x86__is_snc_supported(void)
+{
+       pthread_once(&snc_support_once, init_snc_support);
+       return cached_snc_supported;
 }
 
 static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
@@ -52,49 +64,58 @@ static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
        return cpus;
 }
 
-static int snc_nodes_per_l3_cache(void)
+static int cached_snc_nodes;
+static pthread_once_t snc_nodes_once = PTHREAD_ONCE_INIT;
+
+static void init_snc_nodes(void)
 {
-       static bool checked_snc;
-       static int snc_nodes;
-
-       if (!checked_snc) {
-               struct perf_cpu_map *node_cpus =
-                       read_sysfs_cpu_map("devices/system/node/node0/cpulist");
-               struct perf_cpu_map *cache_cpus =
-                       read_sysfs_cpu_map("devices/system/cpu/cpu0/cache/index3/shared_cpu_list");
-
-               snc_nodes = perf_cpu_map__nr(cache_cpus) / perf_cpu_map__nr(node_cpus);
-               perf_cpu_map__put(cache_cpus);
-               perf_cpu_map__put(node_cpus);
-               checked_snc = true;
-       }
-       return snc_nodes;
+       struct perf_cpu_map *node_cpus =
+               read_sysfs_cpu_map("devices/system/node/node0/cpulist");
+       struct perf_cpu_map *cache_cpus =
+               read_sysfs_cpu_map("devices/system/cpu/cpu0/cache/index3/shared_cpu_list");
+
+       if (node_cpus && cache_cpus)
+               cached_snc_nodes = perf_cpu_map__nr(cache_cpus) / perf_cpu_map__nr(node_cpus);
+       else
+               cached_snc_nodes = 0;
+       perf_cpu_map__put(cache_cpus);
+       perf_cpu_map__put(node_cpus);
 }
 
-static int num_chas(void)
+static int snc_nodes_per_l3_cache(void)
 {
-       static bool checked_chas;
-       static int num_chas;
+       pthread_once(&snc_nodes_once, init_snc_nodes);
+       return cached_snc_nodes;
+}
 
-       if (!checked_chas) {
-               int fd = perf_pmu__event_source_devices_fd();
-               struct io_dir dir;
-               struct io_dirent64 *dent;
+static int cached_num_chas;
+static pthread_once_t num_chas_once = PTHREAD_ONCE_INIT;
 
-               if (fd < 0)
-                       return -1;
+static void init_num_chas(void)
+{
+       int fd = perf_pmu__event_source_devices_fd();
+       struct io_dir dir;
+       struct io_dirent64 *dent;
 
-               io_dir__init(&dir, fd);
+       if (fd < 0) {
+               cached_num_chas = -1;
+               return;
+       }
 
-               while ((dent = io_dir__readdir(&dir)) != NULL) {
-                       /* Note, dent->d_type will be DT_LNK and so isn't a useful filter. */
-                       if (strstarts(dent->d_name, "uncore_cha_"))
-                               num_chas++;
-               }
-               close(fd);
-               checked_chas = true;
+       io_dir__init(&dir, fd);
+
+       while ((dent = io_dir__readdir(&dir)) != NULL) {
+               /* Note, dent->d_type will be DT_LNK and so isn't a useful filter. */
+               if (strstarts(dent->d_name, "uncore_cha_"))
+                       cached_num_chas++;
        }
-       return num_chas;
+       close(fd);
+}
+
+static int num_chas(void)
+{
+       pthread_once(&num_chas_once, init_num_chas);
+       return cached_num_chas;
 }
 
 #define MAX_SNCS 6
@@ -121,46 +142,73 @@ static int uncore_cha_snc(struct perf_pmu *pmu)
                return 0;
        }
        chas_per_node = num_cha / snc_nodes;
+       if (chas_per_node == 0) {
+               pr_warning("Unexpected: chas_per_node is 0 (num_cha=%d, snc_nodes=%d)\n",
+                          num_cha, snc_nodes);
+               return 0;
+       }
        cha_snc = cha_num / chas_per_node;
 
        /* Range check cha_snc. for unexpected out of bounds. */
        return cha_snc >= MAX_SNCS ? 0 : cha_snc;
 }
 
-static int uncore_imc_snc(struct perf_pmu *pmu)
+static const u8 *cached_imc_snc_map;
+static size_t cached_imc_snc_map_len;
+static pthread_once_t imc_snc_map_once = PTHREAD_ONCE_INIT;
+
+static void init_snc_map(void)
 {
-       // Compute the IMC SNC using lookup tables.
-       unsigned int imc_num;
        int snc_nodes = snc_nodes_per_l3_cache();
-       const u8 snc2_map[] = {1, 1, 0, 0, 1, 1, 0, 0};
-       const u8 snc3_map[] = {1, 1, 0, 0, 2, 2, 1, 1, 0, 0, 2, 2};
-       const u8 *snc_map;
-       size_t snc_map_len;
+       char *cpuid;
+       static const u8 gnr_snc2_map[] = { 1, 1, 0, 0 };
+       static const u8 snc3_map[] = { 1, 1, 0, 0, 2, 2 };
 
        switch (snc_nodes) {
        case 2:
-               snc_map = snc2_map;
-               snc_map_len = ARRAY_SIZE(snc2_map);
+               cpuid = get_cpuid_str((struct perf_cpu){ 0 });
+               if (cpuid) {
+                       if (strcmp_cpuid_str(GENUINE_INTEL_GNR, cpuid) == 0) {
+                               cached_imc_snc_map = gnr_snc2_map;
+                               cached_imc_snc_map_len = ARRAY_SIZE(gnr_snc2_map);
+                       }
+                       free(cpuid);
+               }
                break;
        case 3:
-               snc_map = snc3_map;
-               snc_map_len = ARRAY_SIZE(snc3_map);
+               cached_imc_snc_map = snc3_map;
+               cached_imc_snc_map_len = ARRAY_SIZE(snc3_map);
                break;
        default:
                /* Error or no lookup support for SNC with >3 nodes. */
-               return 0;
+               break;
        }
 
+       if (!cached_imc_snc_map)
+               pr_warning("Unexpected: can not find snc map config\n");
+}
+
+static int uncore_imc_snc(struct perf_pmu *pmu)
+{
+       // Compute the IMC SNC using lookup tables.
+       unsigned int imc_num;
+       int snc_nodes = snc_nodes_per_l3_cache();
+
+       if (snc_nodes <= 1)
+               return 0;
+
+       pthread_once(&imc_snc_map_once, init_snc_map);
+
        /* Compute SNC for PMU. */
        if (sscanf(pmu->name, "uncore_imc_%u", &imc_num) != 1) {
                pr_warning("Unexpected: unable to compute IMC number '%s'\n", pmu->name);
                return 0;
        }
-       if (imc_num >= snc_map_len) {
-               pr_warning("Unexpected IMC %d for SNC%d mapping\n", imc_num, snc_nodes);
+
+       if (!cached_imc_snc_map)
                return 0;
-       }
-       return snc_map[imc_num];
+
+       return cached_imc_snc_map[imc_num % cached_imc_snc_map_len];
 }
 
 static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
@@ -200,7 +248,9 @@ static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
        return cpu_adjust[pmu_snc];
 }
 
-static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
+static pthread_mutex_t pmu_adjust_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
 {
        // With sub-NUMA clustering (SNC) there is a NUMA node per SNC in the
        // topology. For example, a two socket graniterapids machine may be set
@@ -231,9 +281,11 @@ static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool
                return;
        }
 
+       pthread_mutex_lock(&pmu_adjust_mutex);
+
        pmu_snc = cha ? uncore_cha_snc(pmu) : uncore_imc_snc(pmu);
        if (pmu_snc == 0) {
-               // No adjustment necessary for the first SNC.
+               pthread_mutex_unlock(&pmu_adjust_mutex);
                return;
        }
 
@@ -242,8 +294,10 @@ static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool
                // Hold onto the perf_cpu_map globally to avoid recomputation.
                cpu_adjust = uncore_cha_imc_compute_cpu_adjust(pmu_snc);
                adjusted[pmu_snc] = perf_cpu_map__empty_new(perf_cpu_map__nr(pmu->cpus));
-               if (!adjusted[pmu_snc])
+               if (!adjusted[pmu_snc]) {
+                       pthread_mutex_unlock(&pmu_adjust_mutex);
                        return;
+               }
        }
 
        perf_cpu_map__for_each_cpu(cpu, idx, pmu->cpus) {
@@ -263,6 +317,8 @@ static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool
 
        perf_cpu_map__put(pmu->cpus);
        pmu->cpus = perf_cpu_map__get(adjusted[pmu_snc]);
+
+       pthread_mutex_unlock(&pmu_adjust_mutex);
 }
 
 void perf_pmu__arch_init(struct perf_pmu *pmu)
@@ -300,11 +356,16 @@ void perf_pmu__arch_init(struct perf_pmu *pmu)
                                pmu->mem_events = perf_mem_events_intel_aux;
                        else
                                pmu->mem_events = perf_mem_events_intel;
-               } else if (x86__is_intel_graniterapids()) {
-                       if (strstarts(pmu->name, "uncore_cha_"))
-                               gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
-                       else if (strstarts(pmu->name, "uncore_imc_"))
-                               gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+               } else if (x86__is_snc_supported()) {
+                       int snc_nodes = snc_nodes_per_l3_cache();
+
+                       if (snc_nodes == 2 || snc_nodes == 3) {
+                               if (strstarts(pmu->name, "uncore_cha_"))
+                                       uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
+                               else if (strstarts(pmu->name, "uncore_imc_") &&
+                                        !strstarts(pmu->name, "uncore_imc_free_running"))
+                                       uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+                       }
                }
        }
 }
--

2.54.0.823.g6e5bcc1fc9-goog