Skip to contentSkip to conversation

powerpc/perf: Set cpumode flags using sample address

PATCH Superseded Anjali K 2 participants 3 messages May 17, 2024 – May 27, 2024 PowerPC

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Anjali K

author
#0 lore raw

PATCH v2 1/1 powerpc/perf: Set cpumode flags using sample address

Currently in some cases, when the sampled instruction address register latches to a specific address during sampling, there is an inconsistency in the privilege bits captured in the sampled event register. For example, a snippet from the perf report on a power10 system is:

Overhead  Address             Command       Shared Object      Symbol
........  ..................  ............  .................  .......................
     2.41%  0x7fff9f94a02c      null_syscall  [unknown]          [k] 0x00007fff9f94a02c
     2.20%  0x7fff9f94a02c      null_syscall  libc.so.6          [.] syscall

perf_get_misc_flags() function looks at the privilege bits to return the corresponding flags to be used for the address symbol and these privilege bit details are read from the sampled event register. In the above snippet, address "0x00007fff9f94a02c" is shown as "k" (kernel) due to the inconsistent privilege bits captured in the sampled event register. To address this case, the proposed fix is to additionally check whether the sampled address is in the kernel area. Since this is specific to the latest platform, a new pmu flag is added called "PPMU_P10" and is used to contain the proposed fix.

---
Changelog:
V1->V2:
Fixed the build warning reported by the kernel test bot
Added a new flag PPMU_P10 and used it instead of PPMU_ARCH_31 to restrict the changes to the current platform (Power10)

 arch/powerpc/include/asm/perf_event_server.h |  1 +
 arch/powerpc/perf/core-book3s.c              | 43 ++++++++------------
 arch/powerpc/perf/power10-pmu.c              |  3 +-
 3 files changed, 20 insertions(+), 27 deletions(-)
arch/powerpc/include/asm/perf_event_server.h +1 -0
index e2221d29fdf9..12f7bfb4cab1 100644
@@ -90,6 +90,7 @@ struct power_pmu {
 #define PPMU_ARCH_31		0x00000200 /* Has MMCR3, SIER2 and SIER3 */
 #define PPMU_P10_DD1		0x00000400 /* Is power10 DD1 processor version */
 #define PPMU_HAS_ATTR_CONFIG1	0x00000800 /* Using config1 attribute */
+#define PPMU_P10			0x00001000 /* For power10 pmu */
 
 /*
  * Values for flags to get_alternatives()
arch/powerpc/perf/core-book3s.c +17 -26

67 lines not on this page · raw message

arch/powerpc/perf/power10-pmu.c +2 -1
index 62a68b6b2d4b..bb57b7cfe640 100644
@@ -593,7 +593,8 @@ static struct power_pmu power10_pmu = {
 	.get_mem_weight		= isa207_get_mem_weight,
 	.disable_pmc		= isa207_disable_pmc,
 	.flags			= PPMU_HAS_SIER | PPMU_ARCH_207S |
-				  PPMU_ARCH_31 | PPMU_HAS_ATTR_CONFIG1,
+				  PPMU_ARCH_31 | PPMU_HAS_ATTR_CONFIG1 |
+				  PPMU_P10,
 	.n_generic		= ARRAY_SIZE(power10_generic_events),
 	.generic_events		= power10_generic_events,
 	.cache_events		= &power10_cache_events,

base-commit: dd5a440a31fae6e459c0d6271dddd62825505361

Signature · 1 line
2.39.3
#1 lore raw

Hi Anjali,

Quoted 3 lines · Anjali K

Currently in some cases, when the sampled instruction address register latches to a specific address during sampling, there is an inconsistency in the privilege bits captured in the sampled event register.

I don't really like "inconsistency", it's vague.

The sampled address is correct, and the privilege bits are incorrect.

If someone is offended by that wording you can direct them to me :)

Quoted 11 lines

For example, a snippet from the perf report on a power10 system is:

Overhead  Address             Command       Shared Object      Symbol
........  ..................  ............  .................  .......................
     2.41%  0x7fff9f94a02c      null_syscall  [unknown]          [k] 0x00007fff9f94a02c
     2.20%  0x7fff9f94a02c      null_syscall  libc.so.6          [.] syscall

perf_get_misc_flags() function looks at the privilege bits to return the corresponding flags to be used for the address symbol and these privilege bit details are read from the sampled event register. In the above snippet, address "0x00007fff9f94a02c" is shown as "k" (kernel) due to the inconsistent privilege bits captured in the sampled event register.

"incorrect privilege bits"

Quoted 1 line

To address this case, the proposed fix is to additionally check whether the

"To address this case check whether the"

Quoted 3 lines

sampled address is in the kernel area. Since this is specific to the latest platform, a new pmu flag is added called "PPMU_P10" and is used to contain the proposed fix.

You should explain why this fix replaces the existing P10_DD1 logic.

Quoted 18 lines

---
Changelog:
V1->V2:
Fixed the build warning reported by the kernel test bot
Added a new flag PPMU_P10 and used it instead of PPMU_ARCH_31 to restrict the changes to the current platform (Power10)

 arch/powerpc/include/asm/perf_event_server.h |  1 +
 arch/powerpc/perf/core-book3s.c              | 43 ++++++++------------
 arch/powerpc/perf/power10-pmu.c              |  3 +-
 3 files changed, 20 insertions(+), 27 deletions(-)
arch/powerpc/include/asm/perf_event_server.h +0 -0
index e2221d29fdf9..12f7bfb4cab1 100644

arch/powerpc/include/asm/perf_event_server.h struct power_pmu { · line 93

Quoted 4 lines
arch/powerpc/include/asm/perf_event_server.h +1 -0
@@ -90,6 +90,7 @@ struct power_pmu {
 #define PPMU_ARCH_31		0x00000200 /* Has MMCR3, SIER2 and SIER3 */
 #define PPMU_P10_DD1		0x00000400 /* Is power10 DD1 processor version */
 #define PPMU_HAS_ATTR_CONFIG1	0x00000800 /* Using config1 attribute */
+#define PPMU_P10			0x00001000 /* For power10 pmu */

Can you put PPMU_P10 immediately after PPMU_P10_DD1. It's OK to renumber PPMU_HAS_ATTR_CONFIG1.

arch/powerpc/perf/core-book3s.c static inline u32 perf_flags_from_msr(struct pt_regs *regs) · line 269

Quoted 11 lines
arch/powerpc/perf/core-book3s.c +1 -2
index 6b5f8a94e7d8..8a2677463a73 100644
@@ -266,31 +266,12 @@ static inline u32 perf_flags_from_msr(struct pt_regs *regs)
 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
 {
 	bool use_siar = regs_use_siar(regs);
-	unsigned long mmcra = regs->dsisr;
-	int marked = mmcra & MMCRA_SAMPLE_ENABLE;
+	unsigned long siar = mfspr(SPRN_SIAR);

We shouldn't read SPRN_SIAR until we know it will be used.

Quoted 51 lines
arch/powerpc/perf/core-book3s.c +8 -24
+	unsigned long addr;
 
 	if (!use_siar)
 		return perf_flags_from_msr(regs);
 
-	/*
-	 * Check the address in SIAR to identify the
-	 * privilege levels since the SIER[MSR_HV, MSR_PR]
-	 * bits are not set for marked events in power10
-	 * DD1.
-	 */
-	if (marked && (ppmu->flags & PPMU_P10_DD1)) {
-		unsigned long siar = mfspr(SPRN_SIAR);
-		if (siar) {
-			if (is_kernel_addr(siar))
-				return PERF_RECORD_MISC_KERNEL;
-			return PERF_RECORD_MISC_USER;
-		} else {
-			if (is_kernel_addr(regs->nip))
-				return PERF_RECORD_MISC_KERNEL;
-			return PERF_RECORD_MISC_USER;
-		}
-	}
-
 	/*
 	 * If we don't have flags in MMCRA, rather than using
 	 * the MSR, we intuit the flags from the address in
@@ -298,19 +279,29 @@ static inline u32 perf_get_misc_flags(struct pt_regs *regs)
 	 * results
 	 */
 	if (ppmu->flags & PPMU_NO_SIPR) {
-		unsigned long siar = mfspr(SPRN_SIAR);
 		if (is_kernel_addr(siar))
 			return PERF_RECORD_MISC_KERNEL;
 		return PERF_RECORD_MISC_USER;
 	}
 
 	/* PR has priority over HV, so order below is important */
-	if (regs_sipr(regs))
-		return PERF_RECORD_MISC_USER;
-
-	if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
+	if (regs_sipr(regs)) {
+		if (!(ppmu->flags & PPMU_P10))
+			return PERF_RECORD_MISC_USER;
+	} else if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
 		return PERF_RECORD_MISC_HYPERVISOR;
 
+	/*
+	 * Check the address in SIAR to identify the
+	 * privilege levels since the SIER[MSR_HV, MSR_PR]

arch/powerpc/perf/core-book3s.c static inline u32 perf_get_misc_flags(struct pt_regs *regs) · line 306

Quoted 10 lines
arch/powerpc/perf/core-book3s.c +8 -0
+	 * bits are not set correctly in power10 sometimes
+	 */
+	if (ppmu->flags & PPMU_P10) {
+		addr = siar ? siar : regs->nip;
+		if (!is_kernel_addr(addr))
+			return PERF_RECORD_MISC_USER;
+	}
+
 	return PERF_RECORD_MISC_KERNEL;
 }

cheers

author
#2 lore raw
Quoted 124 lines · Michael Ellerman

Hi Anjali,

Quoted 3 lines · Anjali K

Currently in some cases, when the sampled instruction address register latches to a specific address during sampling, there is an inconsistency in the privilege bits captured in the sampled event register.

I don't really like "inconsistency", it's vague.

The sampled address is correct, and the privilege bits are incorrect.

If someone is offended by that wording you can direct them to me :)

Quoted 11 lines

For example, a snippet from the perf report on a power10 system is:

Overhead  Address             Command       Shared Object      Symbol
........  ..................  ............  .................  .......................
     2.41%  0x7fff9f94a02c      null_syscall  [unknown]          [k] 0x00007fff9f94a02c
     2.20%  0x7fff9f94a02c      null_syscall  libc.so.6          [.] syscall

perf_get_misc_flags() function looks at the privilege bits to return the corresponding flags to be used for the address symbol and these privilege bit details are read from the sampled event register. In the above snippet, address "0x00007fff9f94a02c" is shown as "k" (kernel) due to the inconsistent privilege bits captured in the sampled event register.

"incorrect privilege bits"

Quoted 1 line

To address this case, the proposed fix is to additionally check whether the

"To address this case check whether the"

Quoted 3 lines

sampled address is in the kernel area. Since this is specific to the latest platform, a new pmu flag is added called "PPMU_P10" and is used to contain the proposed fix.

You should explain why this fix replaces the existing P10_DD1 logic.

Quoted 18 lines

---
Changelog:
V1->V2:
Fixed the build warning reported by the kernel test bot
Added a new flag PPMU_P10 and used it instead of PPMU_ARCH_31 to restrict the changes to the current platform (Power10)

 arch/powerpc/include/asm/perf_event_server.h |  1 +
 arch/powerpc/perf/core-book3s.c              | 43 ++++++++------------
 arch/powerpc/perf/power10-pmu.c              |  3 +-
 3 files changed, 20 insertions(+), 27 deletions(-)
arch/powerpc/include/asm/perf_event_server.h +0 -0
index e2221d29fdf9..12f7bfb4cab1 100644

arch/powerpc/include/asm/perf_event_server.h struct power_pmu { · line 93

Quoted 4 lines
arch/powerpc/include/asm/perf_event_server.h +1 -0
@@ -90,6 +90,7 @@ struct power_pmu {
 #define PPMU_ARCH_31		0x00000200 /* Has MMCR3, SIER2 and SIER3 */
 #define PPMU_P10_DD1		0x00000400 /* Is power10 DD1 processor version */
 #define PPMU_HAS_ATTR_CONFIG1	0x00000800 /* Using config1 attribute */
+#define PPMU_P10			0x00001000 /* For power10 pmu */

Can you put PPMU_P10 immediately after PPMU_P10_DD1. It's OK to renumber PPMU_HAS_ATTR_CONFIG1.

arch/powerpc/perf/core-book3s.c static inline u32 perf_flags_from_msr(struct pt_regs *regs) · line 269

Quoted 11 lines
arch/powerpc/perf/core-book3s.c +1 -2
index 6b5f8a94e7d8..8a2677463a73 100644
@@ -266,31 +266,12 @@ static inline u32 perf_flags_from_msr(struct pt_regs *regs)
 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
 {
 	bool use_siar = regs_use_siar(regs);
-	unsigned long mmcra = regs->dsisr;
-	int marked = mmcra & MMCRA_SAMPLE_ENABLE;
+	unsigned long siar = mfspr(SPRN_SIAR);

We shouldn't read SPRN_SIAR until we know it will be used.

Quoted 48 lines
arch/powerpc/perf/core-book3s.c +5 -24
+	unsigned long addr;
 
 	if (!use_siar)
 		return perf_flags_from_msr(regs);
 
-	/*
-	 * Check the address in SIAR to identify the
-	 * privilege levels since the SIER[MSR_HV, MSR_PR]
-	 * bits are not set for marked events in power10
-	 * DD1.
-	 */
-	if (marked && (ppmu->flags & PPMU_P10_DD1)) {
-		unsigned long siar = mfspr(SPRN_SIAR);
-		if (siar) {
-			if (is_kernel_addr(siar))
-				return PERF_RECORD_MISC_KERNEL;
-			return PERF_RECORD_MISC_USER;
-		} else {
-			if (is_kernel_addr(regs->nip))
-				return PERF_RECORD_MISC_KERNEL;
-			return PERF_RECORD_MISC_USER;
-		}
-	}
-
 	/*
 	 * If we don't have flags in MMCRA, rather than using
 	 * the MSR, we intuit the flags from the address in
@@ -298,19 +279,29 @@ static inline u32 perf_get_misc_flags(struct pt_regs *regs)
 	 * results
 	 */
 	if (ppmu->flags & PPMU_NO_SIPR) {
-		unsigned long siar = mfspr(SPRN_SIAR);
 		if (is_kernel_addr(siar))
 			return PERF_RECORD_MISC_KERNEL;
 		return PERF_RECORD_MISC_USER;
 	}
 
 	/* PR has priority over HV, so order below is important */
-	if (regs_sipr(regs))
-		return PERF_RECORD_MISC_USER;
-
-	if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
+	if (regs_sipr(regs)) {
+		if (!(ppmu->flags & PPMU_P10))
+			return PERF_RECORD_MISC_USER;
+	} else if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
 		return PERF_RECORD_MISC_HYPERVISOR;
 
Quoted 14 lines

arch/powerpc/perf/core-book3s.c static inline u32 perf_get_misc_flags(struct pt_regs *regs) · line 306

Quoted 13 lines
arch/powerpc/perf/core-book3s.c +11 -0
+	/*
+	 * Check the address in SIAR to identify the
+	 * privilege levels since the SIER[MSR_HV, MSR_PR]
+	 * bits are not set correctly in power10 sometimes
+	 */
+	if (ppmu->flags & PPMU_P10) {
+		addr = siar ? siar : regs->nip;
+		if (!is_kernel_addr(addr))
+			return PERF_RECORD_MISC_USER;
+	}
+
 	return PERF_RECORD_MISC_KERNEL;
 }

cheers

Thank you for reviewing. I will address the review comments and send the next version of the patch. Anjali K