Skip to contentSkip to conversation

powerpc/perf: Fix branch_filter support for multiple filters in powerpc

PATCH Superseded Athira Rajeev 3 participants 7 messages Sep 13, 2022 – Sep 16, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#3 lore raw

PATCH 3/3 tools/testing/selftests/powerpc: Update the bhrb filter sampling test to test for multiple branch filters

In reply to Athira Rajeev

For PERF_SAMPLE_BRANCH_STACK sample type, different branch_sample_type, ie branch filters are supported. The testcase "bhrb_filter_map_test" tests the valid and invalid filter maps in different powerpc platforms. Update this testcase to include scenario to cover multiple branch filters at sametime. Since powerpc doesn't support multiple filters at sametime, expect failure during perf_event_open.

---
 .../powerpc/pmu/sampling_tests/bhrb_filter_map_test.c    | 9 +++++++++
 1 file changed, 9 insertions(+)
tools/testing/selftests/powerpc/pmu/sampling_tests/bhrb_filter_map_test.c +9 -0
index 8182647c63c8..605669d4e4cb 100644
@@ -96,6 +96,15 @@ static int bhrb_filter_map_test(void)
 		}
 	}
 
+	/*
+	 * Combine filter maps which includes a valid branch filter and an invalid branch
+	 * filter. Example: any ( PERF_SAMPLE_BRANCH_ANY) and save_type
+	 * (PERF_SAMPLE_BRANCH_TYPE_SAVE).
+	 * The perf_event_open should fail in this case.
+	 */
+	event.attr.branch_sample_type = PERF_SAMPLE_BRANCH_ANY | PERF_SAMPLE_BRANCH_TYPE_SAVE;
+	FAIL_IF(!event_open(&event));
+
 	return 0;
 }
 
Signature · 1 line
2.31.1
#1 lore raw

PATCH 2/3 tools/perf/tests: Fix branch stack sampling test to include sanity check for branch filter

In reply to Athira Rajeev

commit b55878c90ab9 ("perf test: Add test for branch stack sampling") added test for branch stack sampling. There is a sanity check in the beginning to skip the test if the hardware doesn't support branch stack sampling.

Snippet
<<>>
skip the test if the hardware doesn't support branch stack sampling perf record -b -o- -B true > /dev/null 2>&1 || exit 2 <<>>

But the testcase also uses branch sample types: save_type, any. if any platform doesn't support the branch filters used in the test, the testcase will fail. In powerpc, currently mutliple branch filters are not supported and hence this test fails in powerpc. Fix the sanity check to look at the support for branch filters used in this test before proceeding with the test.

Fixes b55878c90ab9 ("perf test: Add test for branch stack sampling")
---
 tools/perf/tests/shell/test_brstack.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/tests/shell/test_brstack.sh +2 -1
index c644f94a6500..ec801cffae6b 100755
@@ -12,7 +12,8 @@ if ! [ -x "$(command -v cc)" ]; then
 fi
 
 # skip the test if the hardware doesn't support branch stack sampling
-perf record -b -o- -B true > /dev/null 2>&1 || exit 2
+# and if the architecture doesn't support filter types: any,save_type,u
+perf record -b -o- -B --branch-filter any,save_type,u true > /dev/null 2>&1 || exit 2
 
 TMPDIR=$(mktemp -d /tmp/__perf_test.program.XXXXX)
 
Signature · 1 line
2.31.1
#0 lore raw

PATCH 1/3 powerpc/perf: Fix branch_filter support for multiple filters in powerpc

For PERF_SAMPLE_BRANCH_STACK sample type, different branch_sample_type ie branch filters are supported. The branch filters are requested via event attribute "branch_sample_type". Multiple branch filters can be passed in event attribute.

Example:
perf record -b -o- -B --branch-filter any,ind_call true

Powerpc does not support having multiple branch filters at sametime. In powerpc, branch filters for branch stack sampling is set via MMCRA IFM bits [32:33]. But currently when requesting for multiple filter types, the "perf record" command does not report any error.

Example:
perf record -b -o- -B --branch-filter any,save_type true perf record -b -o- -B --branch-filter any,ind_call true

The "bhrb_filter_map" function in PMU driver code does the validity check for supported branch filters. But this check is done for single filter. Hence "perf record" will proceed here without reporting any error.

Fix power_pmu_event_init to return EOPNOTSUPP when multiple branch filters are requested in the event attr.

After the fix:
perf record --branch-filter any,ind_call -- ls
Error:
cycles: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'

---
 arch/powerpc/perf/core-book3s.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/powerpc/perf/core-book3s.c +15 -0
index 13919eb96931..2c2d235cb8d8 100644
@@ -2131,6 +2131,21 @@ static int power_pmu_event_init(struct perf_event *event)
 	if (has_branch_stack(event)) {
 		u64 bhrb_filter = -1;
 
+		/*
+		 * powerpc does not support having multiple branch filters
+		 * at sametime. Branch filters are set via MMCRA IFM[32:33]
+		 * bits for power8 and above. Return EOPNOTSUPP when multiple
+		 * branch filters are requested in the event attr.
+		 *
+		 * When opening event via perf_event_open, branch_sample_type
+		 * gets adjusted in perf_copy_attr function. Kernel will
+		 * automatically adjust the branch_sample_type based on the
+		 * event modifier settings to include PERF_SAMPLE_BRANCH_PLM_ALL.
+		 * Hence drop the check for PERF_SAMPLE_BRANCH_PLM_ALL.
+		 */
+		if (hweight64(event->attr.branch_sample_type & ~PERF_SAMPLE_BRANCH_PLM_ALL) > 1)
+			return -EOPNOTSUPP;
+
 		if (ppmu->bhrb_filter_map)
 			bhrb_filter = ppmu->bhrb_filter_map(
 					event->attr.branch_sample_type);
Signature · 1 line
2.31.1
Tested-by on v1 1/3
#5 lore raw
Quoted 32 lines · Athira Rajeev

For PERF_SAMPLE_BRANCH_STACK sample type, different branch_sample_type ie branch filters are supported. The branch filters are requested via event attribute "branch_sample_type". Multiple branch filters can be passed in event attribute.

Example:
perf record -b -o- -B --branch-filter any,ind_call true

Powerpc does not support having multiple branch filters at sametime. In powerpc, branch filters for branch stack sampling is set via MMCRA IFM bits [32:33]. But currently when requesting for multiple filter types, the "perf record" command does not report any error.

Example:
perf record -b -o- -B --branch-filter any,save_type true perf record -b -o- -B --branch-filter any,ind_call true

The "bhrb_filter_map" function in PMU driver code does the validity check for supported branch filters. But this check is done for single filter. Hence "perf record" will proceed here without reporting any error.

Fix power_pmu_event_init to return EOPNOTSUPP when multiple branch filters are requested in the event attr.

After the fix:
perf record --branch-filter any,ind_call -- ls
Error:
cycles: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'

Quoted 3 lines
Quoted 30 lines
---
  arch/powerpc/perf/core-book3s.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
arch/powerpc/perf/core-book3s.c +15 -0
index 13919eb96931..2c2d235cb8d8 100644
@@ -2131,6 +2131,21 @@ static int power_pmu_event_init(struct perf_event *event)
  	if (has_branch_stack(event)) {
  		u64 bhrb_filter = -1;
 
+		/*
+		 * powerpc does not support having multiple branch filters
+		 * at sametime. Branch filters are set via MMCRA IFM[32:33]
+		 * bits for power8 and above. Return EOPNOTSUPP when multiple
+		 * branch filters are requested in the event attr.
+		 *
+		 * When opening event via perf_event_open, branch_sample_type
+		 * gets adjusted in perf_copy_attr function. Kernel will
+		 * automatically adjust the branch_sample_type based on the
+		 * event modifier settings to include PERF_SAMPLE_BRANCH_PLM_ALL.
+		 * Hence drop the check for PERF_SAMPLE_BRANCH_PLM_ALL.
+		 */
+		if (hweight64(event->attr.branch_sample_type & ~PERF_SAMPLE_BRANCH_PLM_ALL) > 1)
+			return -EOPNOTSUPP;
+
  		if (ppmu->bhrb_filter_map)
  			bhrb_filter = ppmu->bhrb_filter_map(
  					event->attr.branch_sample_type);
Reviewed-by
#6 lore raw
Quoted 34 lines · Disha Goel
Quoted 32 lines · Athira Rajeev

For PERF_SAMPLE_BRANCH_STACK sample type, different branch_sample_type ie branch filters are supported. The branch filters are requested via event attribute "branch_sample_type". Multiple branch filters can be passed in event attribute.

Example:
perf record -b -o- -B --branch-filter any,ind_call true

Powerpc does not support having multiple branch filters at sametime. In powerpc, branch filters for branch stack sampling is set via MMCRA IFM bits [32:33]. But currently when requesting for multiple filter types, the "perf record" command does not report any error.

Example:
perf record -b -o- -B --branch-filter any,save_type true perf record -b -o- -B --branch-filter any,ind_call true

The "bhrb_filter_map" function in PMU driver code does the validity check for supported branch filters. But this check is done for single filter. Hence "perf record" will proceed here without reporting any error.

Fix power_pmu_event_init to return EOPNOTSUPP when multiple branch filters are requested in the event attr.

After the fix:
perf record --branch-filter any,ind_call -- ls
Error:
cycles: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'

Quoted 5 lines
Quoted 3 lines

Patch looks good to me.

Thanks,
Kajol Jain

Quoted 31 lines
Quoted 31 lines
---
 arch/powerpc/perf/core-book3s.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/powerpc/perf/core-book3s.c +15 -0
index 13919eb96931..2c2d235cb8d8 100644
@@ -2131,6 +2131,21 @@ static int power_pmu_event_init(struct perf_event *event)
 	if (has_branch_stack(event)) {
 		u64 bhrb_filter = -1;
 
+		/*
+		 * powerpc does not support having multiple branch filters
+		 * at sametime. Branch filters are set via MMCRA IFM[32:33]
+		 * bits for power8 and above. Return EOPNOTSUPP when multiple
+		 * branch filters are requested in the event attr.
+		 *
+		 * When opening event via perf_event_open, branch_sample_type
+		 * gets adjusted in perf_copy_attr function. Kernel will
+		 * automatically adjust the branch_sample_type based on the
+		 * event modifier settings to include PERF_SAMPLE_BRANCH_PLM_ALL.
+		 * Hence drop the check for PERF_SAMPLE_BRANCH_PLM_ALL.
+		 */
+		if (hweight64(event->attr.branch_sample_type & ~PERF_SAMPLE_BRANCH_PLM_ALL) > 1)
+			return -EOPNOTSUPP;
+
 		if (ppmu->bhrb_filter_map)
 			bhrb_filter = ppmu->bhrb_filter_map(
 					event->attr.branch_sample_type);
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] tools/perf/tests: Fix branch stack sampling test to include sanity check for branch filter

In reply to Athira Rajeev
Quoted 18 lines · Athira Rajeev

commit b55878c90ab9 ("perf test: Add test for branch stack sampling") added test for branch stack sampling. There is a sanity check in the beginning to skip the test if the hardware doesn't support branch stack sampling.

Snippet
<<>>
skip the test if the hardware doesn't support branch stack sampling perf record -b -o- -B true > /dev/null 2>&1 || exit 2 <<>>

But the testcase also uses branch sample types: save_type, any. if any platform doesn't support the branch filters used in the test, the testcase will fail. In powerpc, currently mutliple branch filters are not supported and hence this test fails in powerpc. Fix the sanity check to look at the support for branch filters used in this test before proceeding with the test.

Quoted 3 lines
Fixes b55878c90ab9 ("perf test: Add test for branch stack sampling")

Patch looks good to me.

Thanks,
Kajol Jain

Quoted 18 lines
---
 tools/perf/tests/shell/test_brstack.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/tests/shell/test_brstack.sh +2 -1
index c644f94a6500..ec801cffae6b 100755
@@ -12,7 +12,8 @@ if ! [ -x "$(command -v cc)" ]; then
 fi
 
 # skip the test if the hardware doesn't support branch stack sampling
-perf record -b -o- -B true > /dev/null 2>&1 || exit 2
+# and if the architecture doesn't support filter types: any,save_type,u
+perf record -b -o- -B --branch-filter any,save_type,u true > /dev/null 2>&1 || exit 2
 
 TMPDIR=$(mktemp -d /tmp/__perf_test.program.XXXXX)
 
Reviewed-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] tools/testing/selftests/powerpc: Update the bhrb filter sampling test to test for multiple branch filters

In reply to Athira Rajeev
Quoted 18 lines · Athira Rajeev

For PERF_SAMPLE_BRANCH_STACK sample type, different branch_sample_type, ie branch filters are supported. The testcase "bhrb_filter_map_test" tests the valid and invalid filter maps in different powerpc platforms. Update this testcase to include scenario to cover multiple branch filters at sametime. Since powerpc doesn't support multiple filters at sametime, expect failure during perf_event_open.

---
 .../powerpc/pmu/sampling_tests/bhrb_filter_map_test.c    | 9 +++++++++
 1 file changed, 9 insertions(+)
tools/testing/selftests/powerpc/pmu/sampling_tests/bhrb_filter_map_test.c +0 -0
index 8182647c63c8..605669d4e4cb 100644

tools/testing/selftests/powerpc/pmu/sampling_tests/bhrb_filter_map_test.c static int bhrb_filter_map_test(void) · line 98

Quoted 3 lines
tools/testing/selftests/powerpc/pmu/sampling_tests/bhrb_filter_map_test.c +0 -0
@@ -96,6 +96,15 @@ static int bhrb_filter_map_test(void)
 		}
 	}
 

Patch looks good to me.

Thanks,
Kajol Jain

Quoted 12 lines
tools/testing/selftests/powerpc/pmu/sampling_tests/bhrb_filter_map_test.c +9 -0
+	/*
+	 * Combine filter maps which includes a valid branch filter and an invalid branch
+	 * filter. Example: any ( PERF_SAMPLE_BRANCH_ANY) and save_type
+	 * (PERF_SAMPLE_BRANCH_TYPE_SAVE).
+	 * The perf_event_open should fail in this case.
+	 */
+	event.attr.branch_sample_type = PERF_SAMPLE_BRANCH_ANY | PERF_SAMPLE_BRANCH_TYPE_SAVE;
+	FAIL_IF(!event_open(&event));
+
 	return 0;
 }