Skip to contentSkip to conversation

perf test: Fixes for check branch stack sampling

PATCH Superseded Ian Rogers 4 participants 14 messages Apr 8 – Apr 14 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf test: Fixes for check branch stack sampling

When filtering branch stack samples on user events they sample in user land but may have come from the kernel. Avoid the target address being a kernel address but allow the source to be the kernel.

When filtering branch stack samples on kernel events they sample in kernel land but may have come from user land. Avoid the target being a user address but allow the source to be in user land.

Increase the duration of the system call sampling test to make the likelihood of sampling a system call higher (increased from 1000 to 8000 loops - a number found through experimentation on an Intel Tigerlake laptop), also make the period of the event a prime number.

Put unneeded perf record output into a temporary file so that the test output isn't cluttered. More clearly state which test is running and the pass, fail or skipped result of the test.

These changes make the test on an Intel tigerlake laptop reliably pass rather than reliably fail.

---
 tools/perf/tests/shell/test_brstack.sh | 122 ++++++++++++++++---------
 1 file changed, 78 insertions(+), 44 deletions(-)
tools/perf/tests/shell/test_brstack.sh +78 -44
index 85233d435be6..d317ed0e063e 100755
@@ -40,7 +40,7 @@ is_arm64() {
 
 check_branches() {
 	if ! tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep -E -m1 -q "$1"; then
-		echo "Branches missing $1"
+		echo "ERROR: Branches missing $1"
 		err=1
 	fi
 }
@@ -48,6 +48,8 @@ check_branches() {
 test_user_branches() {
 	echo "Testing user branch stack sampling"
 
+	start_err=$err
+	err=0
 	perf record -o "$TMPDIR/perf.data" --branch-filter any,save_type,u -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
 	perf script -i "$TMPDIR/perf.data" --fields brstacksym > "$TMPDIR/perf.script"
 
@@ -73,59 +75,81 @@ test_user_branches() {
 	perf script -i "$TMPDIR/perf.data" --fields brstack | \
 		tr ' ' '\n' > "$TMPDIR/perf.script"
 
-	# There should be no kernel addresses with the u option, in either
-	# source or target addresses.
-	if grep -E -m1 "0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-		echo "ERROR: Kernel address found in user mode"
+	# There should be no kernel addresses in the target with the u option.
+	if grep -q -E -m1 "^0x[0-9a-f]{16}/[89a-f][0-9a-f]{15}/" $TMPDIR/perf.script; then
+		echo "Testing user branch stack sampling [Failed kernel address found in user mode]"
 		err=1
 	fi
 	# some branch types are still not being tested:
 	# IND COND_CALL COND_RET SYSRET SERROR NO_TX
+	if [ $err -eq 0 ]; then
+		echo "Testing user branch stack sampling [Passed]"
+		err=$start_err
+	else
+		echo "Testing user branch stack sampling [Failed]"
+	fi
 }
 
 test_trap_eret_branches() {
 	echo "Testing trap & eret branches"
+
 	if ! is_arm64; then
-		echo "skip: not arm64"
+		echo "Testing trap & eret branches [Skipped not arm64]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
+		perf test -w traploop 1000
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
+	check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
+	check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+	if [ $err -eq 0 ]; then
+		echo "Testing trap & eret branches [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
-			perf test -w traploop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
-		check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
-		check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+		echo "Testing trap & eret branches [Failed]"
 	fi
 }
 
 test_kernel_branches() {
 	echo "Testing that k option only includes kernel source addresses"
 
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing that k option [Skipped not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
+		perf bench syscall basic --loop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstack | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# Example of branch entries:
+	#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
+	# Source addresses come first and target address can be either
+	# userspace or kernel even with k option, as long as the source
+	# is in kernel.
+
+	# Look for source addresses with top bit set
+	if ! grep -q -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
+		echo "Testing that k option [Failed kernel branches missing]"
+		err=1
+	fi
+	# Look for no target addresses without top bit set
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{0,15}/" $TMPDIR/perf.script; then
+		echo "Testing that k option [Failed user branches found with kernel filter"
+		err=1
+	fi
+	if [ $err -eq 0 ]; then
+		echo "Testing that k option [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstack | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# Example of branch entries:
-		#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
-		# Source addresses come first and target address can be either
-		# userspace or kernel even with k option, as long as the source
-		# is in kernel.
-
-		#Look for source addresses with top bit set
-		if ! grep -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-			echo "ERROR: Kernel branches missing"
-			err=1
-		fi
-		# Look for no source addresses without top bit set
-		if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
-			echo "ERROR: User branches found with kernel filter"
-			err=1
-		fi
+		echo "Testing that k option [Failed]"
 	fi
 }
 
@@ -136,14 +160,15 @@ test_filter() {
 	test_filter_expect=$2
 
 	echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
-	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- ${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
+	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- \
+		${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
 	perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.script"
 
 	# fail if we find any branch type that doesn't match any of the expected ones
 	# also consider UNKNOWN branch types (-)
 	if [ ! -s "$TMPDIR/perf.script" ]
 	then
-		echo "Empty script output"
+		echo "Testing branch stack filtering [Failed empty script output]"
 		err=1
 		return
 	fi
@@ -154,27 +179,36 @@ test_filter() {
 	  > "$TMPDIR/perf.script-filtered" || true
 	if [ -s "$TMPDIR/perf.script-filtered" ]
 	then
-		echo "Unexpected branch filter in script output"
+		echo "Testing branch stack filtering [Failed unexpected branch filter]"
 		cat "$TMPDIR/perf.script"
 		err=1
 		return
 	fi
+	echo "Testing branch stack filtering [Passed]"
 }
 
 test_syscall() {
 	echo "Testing syscalls"
+	start_err=$err
+	err=0
 	# skip if perf doesn't have enough privileges
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing syscalls [Skipped: not enough privileges]"
 	else
 		perf record -o $TMPDIR/perf.data --branch-filter \
-			any_call,save_type,u,k -c 10000 -- \
-			perf bench syscall basic --loop 1000
+			any_call,save_type,u,k -c 10007 -- \
+			perf bench syscall basic --loop 8000  > "$TMPDIR/record.txt" 2>&1
 		perf script -i $TMPDIR/perf.data --fields brstacksym | \
 			tr ' ' '\n' > $TMPDIR/perf.script
 
 		check_branches "getppid[^ ]*/SYSCALL/"
 	fi
+	if [ $err -eq 0 ]; then
+		echo "Testing syscalls [Passed]"
+		err=$start_err
+	else
+		echo "Testing syscalls [Failed]"
+	fi
 }
 set -e
 
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#1 lore raw
Sashiko AI review found 3 potential issue(s): - [Medium] Incorrect regex in `test_user_branches()` renders the test a false negative for leaked kernel addresses. - [Low] Contradictory test output in…

Sashiko AI review found 3 potential issue(s):
- [Medium] Incorrect regex in test_user_branches() renders the test a false negative for leaked kernel addresses.
- [Low] Contradictory test output in test_syscall() when skipped.
- [Low] Missing closing bracket in the failure log output of test_kernel_branches().
--

commit 5cc9b2c1d3de6813cdf5dae014b695e4edf82d5d
Author: Ian Rogers <[email protected]>

perf test: Fixes for check branch stack sampling

This commit updates the branch stack sampling tests to filter cross-boundary branches properly and increases the sampling duration to make syscall sampling more reliable. It also redirects unnecessary test output to temporary files to unclutter the logs and improves the pass/fail reporting.

Quoted 4 lines
tools/perf/tests/shell/test_brstack.sh +0 -0
index 85233d435be63..d317ed0e063ef 100755

[ ... ]

tools/perf/tests/shell/test_brstack.sh test_user_branches() { · line 79

Quoted 10 lines
tools/perf/tests/shell/test_brstack.sh +2 -4
@@ -73,59 +75,81 @@ test_user_branches() {
 	perf script -i "$TMPDIR/perf.data" --fields brstack | \
 		tr ' ' '\n' > "$TMPDIR/perf.script"
 
-	# There should be no kernel addresses with the u option, in either
-	# source or target addresses.
-	if grep -E -m1 "0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-		echo "ERROR: Kernel address found in user mode"
+	# There should be no kernel addresses in the target with the u option.
+	if grep -q -E -m1 "^0x[0-9a-f]{16}/[89a-f][0-9a-f]{15}/" $TMPDIR/perf.script; then

The perf script output normally prefixes addresses with 0x, but this regex appears to expect the target address immediately after the slash to lack a 0x prefix.

It also requires the source address to be exactly 16 hex digits, but user space addresses often lack zero-padding and look like 0x400000.

Does this regex fail to match these output formats, preventing it from detecting leaked kernel addresses in the test?

Quoted 3 lines
tools/perf/tests/shell/test_brstack.sh +1 -0
+		echo "Testing user branch stack sampling [Failed kernel address found in user mode]"
 		err=1
 	fi

[ ... ]

Quoted 3 lines

test_kernel_branches() {
echo "Testing that k option only includes kernel source addresses"

[ ... ]

tools/perf/tests/shell/test_brstack.sh test_user_branches() { · line 145

Quoted 3 lines
tools/perf/tests/shell/test_brstack.sh +3 -0
+	# Look for no target addresses without top bit set
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{0,15}/" $TMPDIR/perf.script; then
+		echo "Testing that k option [Failed user branches found with kernel filter"

This isn't a bug, but there is a missing closing ] bracket at the end of the failure string.

Quoted 2 lines
tools/perf/tests/shell/test_brstack.sh +2 -0
+		err=1
+	fi

[ ... ]

Quoted 12 lines

test_syscall() {

tools/perf/tests/shell/test_brstack.sh +4 -3
 	echo "Testing syscalls"
+	start_err=$err
+	err=0
 	# skip if perf doesn't have enough privileges
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing syscalls [Skipped: not enough privileges]"
 	else
 		perf record -o $TMPDIR/perf.data --branch-filter \
-			any_call,save_type,u,k -c 10000 -- \

tools/perf/tests/shell/test_brstack.sh test_filter() { · line 207

Quoted 10 lines
tools/perf/tests/shell/test_brstack.sh +4 -1
-			perf bench syscall basic --loop 1000
+			any_call,save_type,u,k -c 10007 -- \
+			perf bench syscall basic --loop 8000  > "$TMPDIR/record.txt" 2>&1
 		perf script -i $TMPDIR/perf.data --fields brstacksym | \
 			tr ' ' '\n' > $TMPDIR/perf.script
 
 		check_branches "getppid[^ ]*/SYSCALL/"
 	fi
+	if [ $err -eq 0 ]; then
+		echo "Testing syscalls [Passed]"

Unlike test_kernel_branches(), there is no early return after the skip message.

If perf record lacks privileges, will the test execution fall through to here and incorrectly print that the test passed immediately after printing that it was skipped?

Quoted 5 lines
tools/perf/tests/shell/test_brstack.sh +4 -0
+		err=$start_err
+	else
+		echo "Testing syscalls [Failed]"
+	fi
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#2 lore raw

PATCH v2 1/1 perf test: Fixes for check branch stack sampling

When filtering branch stack samples on user events they sample in user land but may have come from the kernel. Avoid the target address being a kernel address but allow the source to be the kernel.

When filtering branch stack samples on kernel events they sample in kernel land but may have come from user land. Avoid the target being a user address but allow the source to be in user land.

Increase the duration of the system call sampling test to make the likelihood of sampling a system call higher (increased from 1000 to 8000 loops - a number found through experimentation on an Intel Tigerlake laptop), also make the period of the event a prime number.

Put unneeded perf record output into a temporary file so that the test output isn't cluttered. More clearly state which test is running and the pass, fail or skipped result of the test.

These changes make the test on an Intel tigerlake laptop reliably pass rather than reliably fail.

---
 tools/perf/tests/shell/test_brstack.sh | 133 ++++++++++++++++---------
 1 file changed, 84 insertions(+), 49 deletions(-)
tools/perf/tests/shell/test_brstack.sh +84 -49
index 85233d435be6..51cd1dcd1855 100755
@@ -40,7 +40,7 @@ is_arm64() {
 
 check_branches() {
 	if ! tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep -E -m1 -q "$1"; then
-		echo "Branches missing $1"
+		echo "ERROR: Branches missing $1"
 		err=1
 	fi
 }
@@ -48,6 +48,8 @@ check_branches() {
 test_user_branches() {
 	echo "Testing user branch stack sampling"
 
+	start_err=$err
+	err=0
 	perf record -o "$TMPDIR/perf.data" --branch-filter any,save_type,u -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
 	perf script -i "$TMPDIR/perf.data" --fields brstacksym > "$TMPDIR/perf.script"
 
@@ -73,59 +75,81 @@ test_user_branches() {
 	perf script -i "$TMPDIR/perf.data" --fields brstack | \
 		tr ' ' '\n' > "$TMPDIR/perf.script"
 
-	# There should be no kernel addresses with the u option, in either
-	# source or target addresses.
-	if grep -E -m1 "0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-		echo "ERROR: Kernel address found in user mode"
+	# There should be no kernel addresses in the target with the u option.
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[89a-f][0-9a-f]{15}/" $TMPDIR/perf.script; then
+		echo "Testing user branch stack sampling [Failed kernel address found in user mode]"
 		err=1
 	fi
 	# some branch types are still not being tested:
 	# IND COND_CALL COND_RET SYSRET SERROR NO_TX
+	if [ $err -eq 0 ]; then
+		echo "Testing user branch stack sampling [Passed]"
+		err=$start_err
+	else
+		echo "Testing user branch stack sampling [Failed]"
+	fi
 }
 
 test_trap_eret_branches() {
 	echo "Testing trap & eret branches"
+
 	if ! is_arm64; then
-		echo "skip: not arm64"
+		echo "Testing trap & eret branches [Skipped not arm64]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
+		perf test -w traploop 1000
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
+	check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
+	check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+	if [ $err -eq 0 ]; then
+		echo "Testing trap & eret branches [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
-			perf test -w traploop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
-		check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
-		check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+		echo "Testing trap & eret branches [Failed]"
 	fi
 }
 
 test_kernel_branches() {
 	echo "Testing that k option only includes kernel source addresses"
 
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing that k option [Skipped not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
+		perf bench syscall basic --loop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstack | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# Example of branch entries:
+	#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
+	# Source addresses come first and target address can be either
+	# userspace or kernel even with k option, as long as the source
+	# is in kernel.
+
+	# Look for source addresses with top bit set
+	if ! grep -q -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
+		echo "Testing that k option [Failed kernel branches missing]"
+		err=1
+	fi
+	# Look for no target addresses without top bit set
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{0,15}/" $TMPDIR/perf.script; then
+		echo "Testing that k option [Failed user branches found with kernel filter]"
+		err=1
+	fi
+	if [ $err -eq 0 ]; then
+		echo "Testing that k option [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstack | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# Example of branch entries:
-		#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
-		# Source addresses come first and target address can be either
-		# userspace or kernel even with k option, as long as the source
-		# is in kernel.
-
-		#Look for source addresses with top bit set
-		if ! grep -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-			echo "ERROR: Kernel branches missing"
-			err=1
-		fi
-		# Look for no source addresses without top bit set
-		if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
-			echo "ERROR: User branches found with kernel filter"
-			err=1
-		fi
+		echo "Testing that k option [Failed]"
 	fi
 }
 
@@ -136,14 +160,15 @@ test_filter() {
 	test_filter_expect=$2
 
 	echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
-	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- ${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
+	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- \
+		${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
 	perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.script"
 
 	# fail if we find any branch type that doesn't match any of the expected ones
 	# also consider UNKNOWN branch types (-)
 	if [ ! -s "$TMPDIR/perf.script" ]
 	then
-		echo "Empty script output"
+		echo "Testing branch stack filtering [Failed empty script output]"
 		err=1
 		return
 	fi
@@ -154,26 +179,36 @@ test_filter() {
 	  > "$TMPDIR/perf.script-filtered" || true
 	if [ -s "$TMPDIR/perf.script-filtered" ]
 	then
-		echo "Unexpected branch filter in script output"
+		echo "Testing branch stack filtering [Failed unexpected branch filter]"
 		cat "$TMPDIR/perf.script"
 		err=1
 		return
 	fi
+	echo "Testing branch stack filtering [Passed]"
 }
 
 test_syscall() {
 	echo "Testing syscalls"
 	# skip if perf doesn't have enough privileges
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing syscalls [Skipped: not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter \
+		any_call,save_type,u,k -c 10007 -- \
+		perf bench syscall basic --loop 8000  > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	check_branches "getppid[^ ]*/SYSCALL/"
+
+	if [ $err -eq 0 ]; then
+		echo "Testing syscalls [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter \
-			any_call,save_type,u,k -c 10000 -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		check_branches "getppid[^ ]*/SYSCALL/"
+		echo "Testing syscalls [Failed]"
 	fi
 }
 set -e
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#3 lore raw
Sashiko AI review found 2 potential issue(s): - [Medium] Outdated comments and output messages in `test_kernel_branches` explicitly contradict the new test logic and the commit message. - [Medium]…

Sashiko AI review found 2 potential issue(s):
- [Medium] Outdated comments and output messages in test_kernel_branches explicitly contradict the new test logic and the commit message.
- [Medium] Missing perf record output redirection in test_trap_eret_branches.
--

commit 123631eaa14de0c5e9e90fa3e9a1064ed9d669af
Author: Ian Rogers <[email protected]>

perf test: Fixes for check branch stack sampling

This commit updates branch stack sampling tests to handle user/kernel boundary crossings. It also increases the duration of the system call sampling test for reliability, and redirects unneeded perf record output to a temporary file.

Quoted 3 lines
tools/perf/tests/shell/test_brstack.sh +0 -0

[ ... ]

tools/perf/tests/shell/test_brstack.sh test_user_branches() { · line 103

Quoted 12 lines

test_trap_eret_branches() {

tools/perf/tests/shell/test_brstack.sh +8 -1
 	echo "Testing trap & eret branches"
+
 	if ! is_arm64; then
-		echo "skip: not arm64"
+		echo "Testing trap & eret branches [Skipped not arm64]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
+		perf test -w traploop 1000

The commit message states that unneeded perf record output is put into a temporary file to avoid cluttering the test output. Was the redirection intentionally omitted for this specific test?

Quoted 2 lines
tools/perf/tests/shell/test_brstack.sh +2 -0
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script

[ ... ]

Quoted 2 lines

test_kernel_branches() {
echo "Testing that k option only includes kernel source addresses"

Does this output message still reflect the test's intent? The commit message states that the test now allows the source to be in user land, but this message says it "only includes kernel source addresses".

Quoted 9 lines
tools/perf/tests/shell/test_brstack.sh +6 -2
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing that k option [Skipped not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0

tools/perf/tests/shell/test_brstack.sh test_user_branches() { · line 136

Quoted 10 lines
tools/perf/tests/shell/test_brstack.sh +10 -0
+	perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
+		perf bench syscall basic --loop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstack | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# Example of branch entries:
+	#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
+	# Source addresses come first and target address can be either
+	# userspace or kernel even with k option, as long as the source
+	# is in kernel.

This isn't a bug, but this comment appears to contradict the new test logic. The new test logic below correctly checks that the target address is in the kernel rather than the source address. Should this comment be updated?

Quoted 11 lines
tools/perf/tests/shell/test_brstack.sh +11 -0
+
+	# Look for source addresses with top bit set
+	if ! grep -q -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
+		echo "Testing that k option [Failed kernel branches missing]"
+		err=1
+	fi
+	# Look for no target addresses without top bit set
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{0,15}/" $TMPDIR/perf.script; then
+		echo "Testing that k option [Failed user branches found with kernel filter]"
+		err=1
+	fi
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#4 lore raw

PATCH v3 1/1 perf test: Fixes for check branch stack sampling

In reply to Ian Rogers

When filtering branch stack samples on user events they sample in user land but may have come from the kernel. Avoid the target address being a kernel address but allow the source to be the kernel.

When filtering branch stack samples on kernel events they sample in kernel land but may have come from user land. Avoid the target being a user address but allow the source to be in user land.

Increase the duration of the system call sampling test to make the likelihood of sampling a system call higher (increased from 1000 to 8000 loops - a number found through experimentation on an Intel Tigerlake laptop), also make the period of the event a prime number.

Put unneeded perf record output into a temporary file so that the test output isn't cluttered. More clearly state which test is running and the pass, fail or skipped result of the test.

These changes make the test on an Intel tigerlake laptop reliably pass rather than reliably fail.

---
 tools/perf/tests/shell/test_brstack.sh | 134 ++++++++++++++++---------
 1 file changed, 84 insertions(+), 50 deletions(-)
tools/perf/tests/shell/test_brstack.sh +84 -50
index 85233d435be6..025ed9d3d110 100755
@@ -40,7 +40,7 @@ is_arm64() {
 
 check_branches() {
 	if ! tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep -E -m1 -q "$1"; then
-		echo "Branches missing $1"
+		echo "ERROR: Branches missing $1"
 		err=1
 	fi
 }
@@ -48,6 +48,8 @@ check_branches() {
 test_user_branches() {
 	echo "Testing user branch stack sampling"
 
+	start_err=$err
+	err=0
 	perf record -o "$TMPDIR/perf.data" --branch-filter any,save_type,u -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
 	perf script -i "$TMPDIR/perf.data" --fields brstacksym > "$TMPDIR/perf.script"
 
@@ -73,59 +75,80 @@ test_user_branches() {
 	perf script -i "$TMPDIR/perf.data" --fields brstack | \
 		tr ' ' '\n' > "$TMPDIR/perf.script"
 
-	# There should be no kernel addresses with the u option, in either
-	# source or target addresses.
-	if grep -E -m1 "0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-		echo "ERROR: Kernel address found in user mode"
+	# There should be no kernel addresses in the target with the u option.
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[89a-f][0-9a-f]{15}/" $TMPDIR/perf.script; then
+		echo "Testing user branch stack sampling [Failed kernel address found in user mode]"
 		err=1
 	fi
 	# some branch types are still not being tested:
 	# IND COND_CALL COND_RET SYSRET SERROR NO_TX
+	if [ $err -eq 0 ]; then
+		echo "Testing user branch stack sampling [Passed]"
+		err=$start_err
+	else
+		echo "Testing user branch stack sampling [Failed]"
+	fi
 }
 
 test_trap_eret_branches() {
 	echo "Testing trap & eret branches"
+
 	if ! is_arm64; then
-		echo "skip: not arm64"
+		echo "Testing trap & eret branches [Skipped not arm64]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
+		perf test -w traploop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
+	check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
+	check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+	if [ $err -eq 0 ]; then
+		echo "Testing trap & eret branches [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
-			perf test -w traploop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
-		check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
-		check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+		echo "Testing trap & eret branches [Failed]"
 	fi
 }
 
 test_kernel_branches() {
-	echo "Testing that k option only includes kernel source addresses"
+	echo "Testing kernel branch sampling"
 
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing that k option [Skipped not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
+		perf bench syscall basic --loop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstack | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# Example of branch entries:
+	#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
+	# Source addresses come first in user or kernel code. Next is the target
+        # address that must be in the kernel.
+
+	# Look for source addresses with top bit set
+	if ! grep -q -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
+		echo "Testing kernel branch sampling [Failed kernel branches missing]"
+		err=1
+	fi
+	# Look for no target addresses without top bit set
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{0,15}/" $TMPDIR/perf.script; then
+		echo "Testing kernel branch sampling [Failed user branches found]"
+		err=1
+	fi
+	if [ $err -eq 0 ]; then
+		echo "Testing kernel branch sampling [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstack | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# Example of branch entries:
-		#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
-		# Source addresses come first and target address can be either
-		# userspace or kernel even with k option, as long as the source
-		# is in kernel.
-
-		#Look for source addresses with top bit set
-		if ! grep -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-			echo "ERROR: Kernel branches missing"
-			err=1
-		fi
-		# Look for no source addresses without top bit set
-		if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
-			echo "ERROR: User branches found with kernel filter"
-			err=1
-		fi
+		echo "Testing kernel branch sampling [Failed]"
 	fi
 }
 
@@ -136,14 +159,15 @@ test_filter() {
 	test_filter_expect=$2
 
 	echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
-	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- ${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
+	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- \
+		${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
 	perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.script"
 
 	# fail if we find any branch type that doesn't match any of the expected ones
 	# also consider UNKNOWN branch types (-)
 	if [ ! -s "$TMPDIR/perf.script" ]
 	then
-		echo "Empty script output"
+		echo "Testing branch stack filtering [Failed empty script output]"
 		err=1
 		return
 	fi
@@ -154,26 +178,36 @@ test_filter() {
 	  > "$TMPDIR/perf.script-filtered" || true
 	if [ -s "$TMPDIR/perf.script-filtered" ]
 	then
-		echo "Unexpected branch filter in script output"
+		echo "Testing branch stack filtering [Failed unexpected branch filter]"
 		cat "$TMPDIR/perf.script"
 		err=1
 		return
 	fi
+	echo "Testing branch stack filtering [Passed]"
 }
 
 test_syscall() {
 	echo "Testing syscalls"
 	# skip if perf doesn't have enough privileges
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing syscalls [Skipped: not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter \
+		any_call,save_type,u,k -c 10007 -- \
+		perf bench syscall basic --loop 8000  > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	check_branches "getppid[^ ]*/SYSCALL/"
+
+	if [ $err -eq 0 ]; then
+		echo "Testing syscalls [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter \
-			any_call,save_type,u,k -c 10000 -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		check_branches "getppid[^ ]*/SYSCALL/"
+		echo "Testing syscalls [Failed]"
 	fi
 }
 set -e
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#5 lore raw
Sashiko AI review found 1 potential issue(s): - [Medium] Incomplete regex in `test_kernel_branches` misses user branch target addresses that don't start with 0-7, causing false negatives. --

Sashiko AI review found 1 potential issue(s):
- [Medium] Incomplete regex in test_kernel_branches misses user branch target addresses that don't start with 0-7, causing false negatives. --

commit 81a20991ebd7cf4f6e494b9b81ee11052acea653
Author: Ian Rogers <[email protected]>

perf test: Fixes for check branch stack sampling

This commit fixes and expands the branch stack sampling tests in perf. It updates user and kernel branch filters to properly check source and target addresses, increases system call sampling duration for reliability, and cleans up test output.

Quoted 4 lines
tools/perf/tests/shell/test_brstack.sh +0 -0
index 85233d435be63..025ed9d3d1105 100755

[ ... ]

Quoted 1 line
tools/perf/tests/shell/test_brstack.sh +0 -0
@@ -73,59 +75,80 @@ test_user_branches() {

[ ... ]

Quoted 1 line

test_kernel_branches() {

[ ... ]

tools/perf/tests/shell/test_brstack.sh test_user_branches() { · line 143

Quoted 2 lines
tools/perf/tests/shell/test_brstack.sh +2 -0
+	# Look for no target addresses without top bit set
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{0,15}/" $TMPDIR/perf.script; then

Does this regex reliably match all user space target addresses?

Because perf script prints brstack addresses without zero-padding, a valid 32-bit user address like 0x8048000 begins with 8 but is much shorter than 16 characters.

Since the regex strictly requires the first character after the prefix to be between 0 and 7, it seems like it might silently miss shorter user addresses that start with 8 through f, potentially causing the test to pass when it should fail.

Quoted 3 lines
tools/perf/tests/shell/test_brstack.sh +3 -0
+		echo "Testing kernel branch sampling [Failed user branches found]"
+		err=1
+	fi
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#6 lore raw
Quoted 29 lines · On Wed, Apr 8, 2026 at 12:16 AM <[email protected]> wrote:

Sashiko AI review found 1 potential issue(s):
- [Medium] Incomplete regex in test_kernel_branches misses user branch target addresses that don't start with 0-7, causing false negatives. --

commit 81a20991ebd7cf4f6e494b9b81ee11052acea653
Author: Ian Rogers <[email protected]>

perf test: Fixes for check branch stack sampling

This commit fixes and expands the branch stack sampling tests in perf. It updates user and kernel branch filters to properly check source and target addresses, increases system call sampling duration for reliability, and cleans up test output.

Quoted 4 lines
tools/perf/tests/shell/test_brstack.sh +0 -0
index 85233d435be63..025ed9d3d1105 100755

[ ... ]

Quoted 1 line
tools/perf/tests/shell/test_brstack.sh +0 -0
@@ -73,59 +75,80 @@ test_user_branches() {

[ ... ]

Quoted 1 line

test_kernel_branches() {

[ ... ]

tools/perf/tests/shell/test_brstack.sh test_user_branches() { · line 143

Quoted 2 lines
tools/perf/tests/shell/test_brstack.sh +2 -0
+     # Look for no target addresses without top bit set
+     if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{0,15}/" $TMPDIR/perf.script; then

Does this regex reliably match all user space target addresses?

Quoted 8 lines

Because perf script prints brstack addresses without zero-padding, a valid 32-bit user address like 0x8048000 begins with 8 but is much shorter than 16 characters.

Since the regex strictly requires the first character after the prefix to be between 0 and 7, it seems like it might silently miss shorter user addresses that start with 8 through f, potentially causing the test to pass when it should fail.

Expanding the test to support 32-bit and 64-bit addresses in the regular expressions is out-of-scope for this series.

Thanks,
Ian

Quoted 6 lines

tools/perf/tests/shell/test_brstack.sh test_user_branches() { · line 146

Quoted 3 lines
tools/perf/tests/shell/test_brstack.sh +3 -0
+             echo "Testing kernel branch sampling [Failed user branches found]"
+             err=1
+     fi

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

reviewer
#7 lore raw
In reply to Ian Rogers
Quoted 3 lines · Ian Rogers

When filtering branch stack samples on user events they sample in user land but may have come from the kernel. Avoid the target address being a kernel address but allow the source to be the kernel.

Doesn't that leak kernel pointers then? I thought they were supposed to be semi secret to not break KASLR? With /u on Arm64 all kernel branch addresses are filtered out or zeroed. I think it might be worth adding an architecture condition with a different test for Intel if the behavior is different. Otherwise we could start leaking pointers on Arm and the test would not detect it.

Quoted 120 lines

When filtering branch stack samples on kernel events they sample in kernel land but may have come from user land. Avoid the target being a user address but allow the source to be in user land.

Increase the duration of the system call sampling test to make the likelihood of sampling a system call higher (increased from 1000 to 8000 loops - a number found through experimentation on an Intel Tigerlake laptop), also make the period of the event a prime number.

Put unneeded perf record output into a temporary file so that the test output isn't cluttered. More clearly state which test is running and the pass, fail or skipped result of the test.

These changes make the test on an Intel tigerlake laptop reliably pass rather than reliably fail.

---
  tools/perf/tests/shell/test_brstack.sh | 134 ++++++++++++++++---------
  1 file changed, 84 insertions(+), 50 deletions(-)
tools/perf/tests/shell/test_brstack.sh +44 -17
index 85233d435be6..025ed9d3d110 100755
@@ -40,7 +40,7 @@ is_arm64() {
 
  check_branches() {
  	if ! tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep -E -m1 -q "$1"; then
-		echo "Branches missing $1"
+		echo "ERROR: Branches missing $1"
  		err=1
  	fi
  }
@@ -48,6 +48,8 @@ check_branches() {
  test_user_branches() {
  	echo "Testing user branch stack sampling"
 
+	start_err=$err
+	err=0
  	perf record -o "$TMPDIR/perf.data" --branch-filter any,save_type,u -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
  	perf script -i "$TMPDIR/perf.data" --fields brstacksym > "$TMPDIR/perf.script"
 
@@ -73,59 +75,80 @@ test_user_branches() {
  	perf script -i "$TMPDIR/perf.data" --fields brstack | \
  		tr ' ' '\n' > "$TMPDIR/perf.script"
 
-	# There should be no kernel addresses with the u option, in either
-	# source or target addresses.
-	if grep -E -m1 "0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-		echo "ERROR: Kernel address found in user mode"
+	# There should be no kernel addresses in the target with the u option.
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[89a-f][0-9a-f]{15}/" $TMPDIR/perf.script; then
+		echo "Testing user branch stack sampling [Failed kernel address found in user mode]"
  		err=1
  	fi
  	# some branch types are still not being tested:
  	# IND COND_CALL COND_RET SYSRET SERROR NO_TX
+	if [ $err -eq 0 ]; then
+		echo "Testing user branch stack sampling [Passed]"
+		err=$start_err
+	else
+		echo "Testing user branch stack sampling [Failed]"
+	fi
  }
 
  test_trap_eret_branches() {
  	echo "Testing trap & eret branches"
+
  	if ! is_arm64; then
-		echo "skip: not arm64"
+		echo "Testing trap & eret branches [Skipped not arm64]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
+		perf test -w traploop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
+	check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
+	check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+	if [ $err -eq 0 ]; then
+		echo "Testing trap & eret branches [Passed]"
+		err=$start_err
  	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
-			perf test -w traploop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
-		check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
-		check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+		echo "Testing trap & eret branches [Failed]"
  	fi
  }
 
  test_kernel_branches() {
-	echo "Testing that k option only includes kernel source addresses"
+	echo "Testing kernel branch sampling"
 
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing that k option [Skipped not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
+		perf bench syscall basic --loop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstack | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# Example of branch entries:
+	#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."

tools/perf/tests/shell/test_brstack.sh test_user_branches() { · line 143

Quoted 10 lines
tools/perf/tests/shell/test_brstack.sh +10 -0
+	# Source addresses come first in user or kernel code. Next is the target
+        # address that must be in the kernel.
+
+	# Look for source addresses with top bit set
+	if ! grep -q -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
+		echo "Testing kernel branch sampling [Failed kernel branches missing]"
+		err=1
+	fi
+	# Look for no target addresses without top bit set
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{0,15}/" $TMPDIR/perf.script; then

Target can be "0x0" for jumps from the kernel back to userspace with only kernel branches turned on. Before we were only looking at source addresses but now it looks at targets the test fails on 0x0 targets on Arm.

I suppose we could special case 0x0? It starts to get a bit horrible to do in bash with regexes though.

The rest of the changes look good.

Quoted 97 lines
tools/perf/tests/shell/test_brstack.sh +30 -33
+		echo "Testing kernel branch sampling [Failed user branches found]"
+		err=1
+	fi
+	if [ $err -eq 0 ]; then
+		echo "Testing kernel branch sampling [Passed]"
+		err=$start_err
  	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstack | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# Example of branch entries:
-		#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
-		# Source addresses come first and target address can be either
-		# userspace or kernel even with k option, as long as the source
-		# is in kernel.
-
-		#Look for source addresses with top bit set
-		if ! grep -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-			echo "ERROR: Kernel branches missing"
-			err=1
-		fi
-		# Look for no source addresses without top bit set
-		if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
-			echo "ERROR: User branches found with kernel filter"
-			err=1
-		fi
+		echo "Testing kernel branch sampling [Failed]"
  	fi
  }
 
@@ -136,14 +159,15 @@ test_filter() {
  	test_filter_expect=$2
 
  	echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
-	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- ${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
+	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- \
+		${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
  	perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.script"
 
  	# fail if we find any branch type that doesn't match any of the expected ones
  	# also consider UNKNOWN branch types (-)
  	if [ ! -s "$TMPDIR/perf.script" ]
  	then
-		echo "Empty script output"
+		echo "Testing branch stack filtering [Failed empty script output]"
  		err=1
  		return
  	fi
@@ -154,26 +178,36 @@ test_filter() {
  	  > "$TMPDIR/perf.script-filtered" || true
  	if [ -s "$TMPDIR/perf.script-filtered" ]
  	then
-		echo "Unexpected branch filter in script output"
+		echo "Testing branch stack filtering [Failed unexpected branch filter]"
  		cat "$TMPDIR/perf.script"
  		err=1
  		return
  	fi
+	echo "Testing branch stack filtering [Passed]"
  }
 
  test_syscall() {
  	echo "Testing syscalls"
  	# skip if perf doesn't have enough privileges
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing syscalls [Skipped: not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter \
+		any_call,save_type,u,k -c 10007 -- \
+		perf bench syscall basic --loop 8000  > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	check_branches "getppid[^ ]*/SYSCALL/"
+
+	if [ $err -eq 0 ]; then
+		echo "Testing syscalls [Passed]"
+		err=$start_err
  	else
-		perf record -o $TMPDIR/perf.data --branch-filter \
-			any_call,save_type,u,k -c 10000 -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		check_branches "getppid[^ ]*/SYSCALL/"
+		echo "Testing syscalls [Failed]"
  	fi
  }
  set -e
author reviewer
#8 lore raw
Quoted 8 lines · James Clark
Quoted 3 lines · Ian Rogers

When filtering branch stack samples on user events they sample in user land but may have come from the kernel. Avoid the target address being a kernel address but allow the source to be the kernel.

Quoted 6 lines

Doesn't that leak kernel pointers then? I thought they were supposed to be semi secret to not break KASLR? With /u on Arm64 all kernel branch addresses are filtered out or zeroed. I think it might be worth adding an architecture condition with a different test for Intel if the behavior is different. Otherwise we could start leaking pointers on Arm and the test would not detect it.

On x86, the filter applies to the LBR target, suggesting a KASLR leak for LBR sources in the kernel. Wrt the test we could say the test fails on x86 due to this, but this is kind of annoying at the moment. I suppose having some kind of flag to say broken sources are allowed and then setting it on x86 would be best.

Quoted 135 lines
Quoted 120 lines

When filtering branch stack samples on kernel events they sample in kernel land but may have come from user land. Avoid the target being a user address but allow the source to be in user land.

Increase the duration of the system call sampling test to make the likelihood of sampling a system call higher (increased from 1000 to 8000 loops - a number found through experimentation on an Intel Tigerlake laptop), also make the period of the event a prime number.

Put unneeded perf record output into a temporary file so that the test output isn't cluttered. More clearly state which test is running and the pass, fail or skipped result of the test.

These changes make the test on an Intel tigerlake laptop reliably pass rather than reliably fail.

---
  tools/perf/tests/shell/test_brstack.sh | 134 ++++++++++++++++---------
  1 file changed, 84 insertions(+), 50 deletions(-)
tools/perf/tests/shell/test_brstack.sh +44 -17
index 85233d435be6..025ed9d3d110 100755
@@ -40,7 +40,7 @@ is_arm64() {
 
  check_branches() {
      if ! tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep -E -m1 -q "$1"; then
-             echo "Branches missing $1"
+             echo "ERROR: Branches missing $1"
              err=1
      fi
  }
@@ -48,6 +48,8 @@ check_branches() {
  test_user_branches() {
      echo "Testing user branch stack sampling"
 
+     start_err=$err
+     err=0
      perf record -o "$TMPDIR/perf.data" --branch-filter any,save_type,u -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
      perf script -i "$TMPDIR/perf.data" --fields brstacksym > "$TMPDIR/perf.script"
 
@@ -73,59 +75,80 @@ test_user_branches() {
      perf script -i "$TMPDIR/perf.data" --fields brstack | \
              tr ' ' '\n' > "$TMPDIR/perf.script"
 
-     # There should be no kernel addresses with the u option, in either
-     # source or target addresses.
-     if grep -E -m1 "0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-             echo "ERROR: Kernel address found in user mode"
+     # There should be no kernel addresses in the target with the u option.
+     if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[89a-f][0-9a-f]{15}/" $TMPDIR/perf.script; then
+             echo "Testing user branch stack sampling [Failed kernel address found in user mode]"
              err=1
      fi
      # some branch types are still not being tested:
      # IND COND_CALL COND_RET SYSRET SERROR NO_TX
+     if [ $err -eq 0 ]; then
+             echo "Testing user branch stack sampling [Passed]"
+             err=$start_err
+     else
+             echo "Testing user branch stack sampling [Failed]"
+     fi
  }
 
  test_trap_eret_branches() {
      echo "Testing trap & eret branches"
+
      if ! is_arm64; then
-             echo "skip: not arm64"
+             echo "Testing trap & eret branches [Skipped not arm64]"
+             return
+     fi
+     start_err=$err
+     err=0
+     perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
+             perf test -w traploop 1000 > "$TMPDIR/record.txt" 2>&1
+     perf script -i $TMPDIR/perf.data --fields brstacksym | \
+             tr ' ' '\n' > $TMPDIR/perf.script
+
+     # BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
+     check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
+     check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+     if [ $err -eq 0 ]; then
+             echo "Testing trap & eret branches [Passed]"
+             err=$start_err
      else
-             perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
-                     perf test -w traploop 1000
-             perf script -i $TMPDIR/perf.data --fields brstacksym | \
-                     tr ' ' '\n' > $TMPDIR/perf.script
-
-             # BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
-             check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
-             check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+             echo "Testing trap & eret branches [Failed]"
      fi
  }
 
  test_kernel_branches() {
-     echo "Testing that k option only includes kernel source addresses"
+     echo "Testing kernel branch sampling"
 
-     if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-             echo "skip: not enough privileges"
+     if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+             echo "Testing that k option [Skipped not enough privileges]"
+             return
+     fi
+     start_err=$err
+     err=0
+     perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
+             perf bench syscall basic --loop 1000 > "$TMPDIR/record.txt" 2>&1
+     perf script -i $TMPDIR/perf.data --fields brstack | \
+             tr ' ' '\n' > $TMPDIR/perf.script
+
+     # Example of branch entries:
+     #       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."

tools/perf/tests/shell/test_brstack.sh test_user_branches() { · line 143

Quoted 10 lines
tools/perf/tests/shell/test_brstack.sh +10 -0
+     # Source addresses come first in user or kernel code. Next is the target
+        # address that must be in the kernel.
+
+     # Look for source addresses with top bit set
+     if ! grep -q -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
+             echo "Testing kernel branch sampling [Failed kernel branches missing]"
+             err=1
+     fi
+     # Look for no target addresses without top bit set
+     if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{0,15}/" $TMPDIR/perf.script; then

Target can be "0x0" for jumps from the kernel back to userspace with only kernel branches turned on. Before we were only looking at source addresses but now it looks at targets the test fails on 0x0 targets on Arm.

Quoted 2 lines

I suppose we could special case 0x0? It starts to get a bit horrible to do in bash with regexes though.

I wonder, then, if the target should require two numbers, like this:

```
if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{1,15}/"
$TMPDIR/perf.script; then
```
Quoted 1 line

The rest of the changes look good.

Thanks,
Ian

Quoted 98 lines
Quoted 97 lines
tools/perf/tests/shell/test_brstack.sh +30 -33
+             echo "Testing kernel branch sampling [Failed user branches found]"
+             err=1
+     fi
+     if [ $err -eq 0 ]; then
+             echo "Testing kernel branch sampling [Passed]"
+             err=$start_err
      else
-             perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
-                     perf bench syscall basic --loop 1000
-             perf script -i $TMPDIR/perf.data --fields brstack | \
-                     tr ' ' '\n' > $TMPDIR/perf.script
-
-             # Example of branch entries:
-             #       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
-             # Source addresses come first and target address can be either
-             # userspace or kernel even with k option, as long as the source
-             # is in kernel.
-
-             #Look for source addresses with top bit set
-             if ! grep -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-                     echo "ERROR: Kernel branches missing"
-                     err=1
-             fi
-             # Look for no source addresses without top bit set
-             if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
-                     echo "ERROR: User branches found with kernel filter"
-                     err=1
-             fi
+             echo "Testing kernel branch sampling [Failed]"
      fi
  }
 
@@ -136,14 +159,15 @@ test_filter() {
      test_filter_expect=$2
 
      echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
-     perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- ${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
+     perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- \
+             ${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
      perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.script"
 
      # fail if we find any branch type that doesn't match any of the expected ones
      # also consider UNKNOWN branch types (-)
      if [ ! -s "$TMPDIR/perf.script" ]
      then
-             echo "Empty script output"
+             echo "Testing branch stack filtering [Failed empty script output]"
              err=1
              return
      fi
@@ -154,26 +178,36 @@ test_filter() {
        > "$TMPDIR/perf.script-filtered" || true
      if [ -s "$TMPDIR/perf.script-filtered" ]
      then
-             echo "Unexpected branch filter in script output"
+             echo "Testing branch stack filtering [Failed unexpected branch filter]"
              cat "$TMPDIR/perf.script"
              err=1
              return
      fi
+     echo "Testing branch stack filtering [Passed]"
  }
 
  test_syscall() {
      echo "Testing syscalls"
      # skip if perf doesn't have enough privileges
-     if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-             echo "skip: not enough privileges"
+     if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+             echo "Testing syscalls [Skipped: not enough privileges]"
+             return
+     fi
+     start_err=$err
+     err=0
+     perf record -o $TMPDIR/perf.data --branch-filter \
+             any_call,save_type,u,k -c 10007 -- \
+             perf bench syscall basic --loop 8000  > "$TMPDIR/record.txt" 2>&1
+     perf script -i $TMPDIR/perf.data --fields brstacksym | \
+             tr ' ' '\n' > $TMPDIR/perf.script
+
+     check_branches "getppid[^ ]*/SYSCALL/"
+
+     if [ $err -eq 0 ]; then
+             echo "Testing syscalls [Passed]"
+             err=$start_err
      else
-             perf record -o $TMPDIR/perf.data --branch-filter \
-                     any_call,save_type,u,k -c 10000 -- \
-                     perf bench syscall basic --loop 1000
-             perf script -i $TMPDIR/perf.data --fields brstacksym | \
-                     tr ' ' '\n' > $TMPDIR/perf.script
-
-             check_branches "getppid[^ ]*/SYSCALL/"
+             echo "Testing syscalls [Failed]"
      fi
  }
  set -e
author reviewer
#9 lore raw

PATCH v4 1/1 perf test: Fixes for check branch stack sampling

When filtering branch stack samples on user events they sample in user land but may have come from the kernel. Aarch64 avoids leaking the kernel address for kaslr reasons but other platforms, for now, don't. Be more permissive in allowing kernel addresses in the source of user branch stacks.

When filtering branch stack samples on kernel events they sample in kernel land but may have come from user land. Avoid the target being a user address but allow the source to be in user land. Aarch64 may not leak the user land addresses (making them 0) but other platforms do. As the kernel address sampling implies privelege, just allow this.

Increase the duration of the system call sampling test to make the likelihood of sampling a system call higher (increased from 1000 to 8000 loops - a number found through experimentation on an Intel Tigerlake laptop), also make the period of the event a prime number.

Put unneeded perf record output into a temporary file so that the test output isn't cluttered. More clearly state which test is running and the pass, fail or skipped result of the test.

These changes make the test on an Intel tigerlake laptop reliably pass rather than reliably fail.

---
v4: Address feedback from James Clark.
v2,v3: Address Sashiko feedback.

---
 tools/perf/tests/shell/test_brstack.sh | 146 ++++++++++++++++---------
 1 file changed, 96 insertions(+), 50 deletions(-)
tools/perf/tests/shell/test_brstack.sh +96 -50
index 85233d435be6..eb5837f82e39 100755
@@ -38,9 +38,13 @@ is_arm64() {
 	[ "$(uname -m)" = "aarch64" ];
 }
 
+has_kaslr_bug() {
+	[ "$(uname -m)" != "aarch64" ];
+}
+
 check_branches() {
 	if ! tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep -E -m1 -q "$1"; then
-		echo "Branches missing $1"
+		echo "ERROR: Branches missing $1"
 		err=1
 	fi
 }
@@ -48,6 +52,8 @@ check_branches() {
 test_user_branches() {
 	echo "Testing user branch stack sampling"
 
+	start_err=$err
+	err=0
 	perf record -o "$TMPDIR/perf.data" --branch-filter any,save_type,u -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
 	perf script -i "$TMPDIR/perf.data" --fields brstacksym > "$TMPDIR/perf.script"
 
@@ -73,59 +79,88 @@ test_user_branches() {
 	perf script -i "$TMPDIR/perf.data" --fields brstack | \
 		tr ' ' '\n' > "$TMPDIR/perf.script"
 
-	# There should be no kernel addresses with the u option, in either
-	# source or target addresses.
-	if grep -E -m1 "0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-		echo "ERROR: Kernel address found in user mode"
+	# There should be no kernel addresses in the target with the u option.
+	local regex="0x[89a-f][0-9a-f]{15}"
+	if has_kaslr_bug; then
+		# If the system has a kaslr bug that may leak kernel addresses
+		# in the source of something like an ERET/SYSRET. Make the regex
+		# more specific and just check the target address is in user
+		# code.
+		regex="^0x[0-9a-f]{0,16}/0x[89a-f][0-9a-f]{15}/"
+	fi
+	if grep -q -E -m1 "$regex" $TMPDIR/perf.script; then
+		echo "Testing user branch stack sampling [Failed kernel address found in user mode]"
 		err=1
 	fi
 	# some branch types are still not being tested:
 	# IND COND_CALL COND_RET SYSRET SERROR NO_TX
+	if [ $err -eq 0 ]; then
+		echo "Testing user branch stack sampling [Passed]"
+		err=$start_err
+	else
+		echo "Testing user branch stack sampling [Failed]"
+	fi
 }
 
 test_trap_eret_branches() {
 	echo "Testing trap & eret branches"
+
 	if ! is_arm64; then
-		echo "skip: not arm64"
+		echo "Testing trap & eret branches [Skipped not arm64]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
+		perf test -w traploop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
+	check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
+	check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+	if [ $err -eq 0 ]; then
+		echo "Testing trap & eret branches [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
-			perf test -w traploop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
-		check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
-		check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+		echo "Testing trap & eret branches [Failed]"
 	fi
 }
 
 test_kernel_branches() {
-	echo "Testing that k option only includes kernel source addresses"
+	echo "Testing kernel branch sampling"
 
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing that k option [Skipped not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
+		perf bench syscall basic --loop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstack | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# Example of branch entries:
+	#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
+	# Source addresses come first in user or kernel code. Next is the target
+	# address that must be in the kernel.
+
+	# Look for source addresses with top bit set
+	if ! grep -q -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
+		echo "Testing kernel branch sampling [Failed kernel branches missing]"
+		err=1
+	fi
+	# Look for no target addresses without top bit set
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{1,15}/" $TMPDIR/perf.script; then
+		echo "Testing kernel branch sampling [Failed user branches found]"
+		err=1
+	fi
+	if [ $err -eq 0 ]; then
+		echo "Testing kernel branch sampling [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstack | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# Example of branch entries:
-		#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
-		# Source addresses come first and target address can be either
-		# userspace or kernel even with k option, as long as the source
-		# is in kernel.
-
-		#Look for source addresses with top bit set
-		if ! grep -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-			echo "ERROR: Kernel branches missing"
-			err=1
-		fi
-		# Look for no source addresses without top bit set
-		if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
-			echo "ERROR: User branches found with kernel filter"
-			err=1
-		fi
+		echo "Testing kernel branch sampling [Failed]"
 	fi
 }
 
@@ -136,14 +171,15 @@ test_filter() {
 	test_filter_expect=$2
 
 	echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
-	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- ${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
+	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- \
+		${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
 	perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.script"
 
 	# fail if we find any branch type that doesn't match any of the expected ones
 	# also consider UNKNOWN branch types (-)
 	if [ ! -s "$TMPDIR/perf.script" ]
 	then
-		echo "Empty script output"
+		echo "Testing branch stack filtering [Failed empty script output]"
 		err=1
 		return
 	fi
@@ -154,26 +190,36 @@ test_filter() {
 	  > "$TMPDIR/perf.script-filtered" || true
 	if [ -s "$TMPDIR/perf.script-filtered" ]
 	then
-		echo "Unexpected branch filter in script output"
+		echo "Testing branch stack filtering [Failed unexpected branch filter]"
 		cat "$TMPDIR/perf.script"
 		err=1
 		return
 	fi
+	echo "Testing branch stack filtering [Passed]"
 }
 
 test_syscall() {
 	echo "Testing syscalls"
 	# skip if perf doesn't have enough privileges
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing syscalls [Skipped: not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter \
+		any_call,save_type,u,k -c 10007 -- \
+		perf bench syscall basic --loop 8000  > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	check_branches "getppid[^ ]*/SYSCALL/"
+
+	if [ $err -eq 0 ]; then
+		echo "Testing syscalls [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter \
-			any_call,save_type,u,k -c 10000 -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		check_branches "getppid[^ ]*/SYSCALL/"
+		echo "Testing syscalls [Failed]"
 	fi
 }
 set -e
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#10 lore raw
Sashiko AI review found 1 potential issue(s): - [Medium] The regular expression to check for user-space target addresses incorrectly uses `{1,15}`, which excludes single-digit hex addresses like…

Sashiko AI review found 1 potential issue(s):
- [Medium] The regular expression to check for user-space target addresses incorrectly uses {1,15}, which excludes single-digit hex addresses like 0x0. --

commit e332f30c098056428ef830d84c8207d356af6ea2
Author: Ian Rogers <[email protected]>

perf test: Fixes for check branch stack sampling

This commit modifies the branch stack sampling tests to be more permissive regarding kernel addresses in user branches and user addresses in kernel branches. It also adjusts the system call sampling test parameters and redirects unnecessary output to a temporary file.

Quoted 5 lines
tools/perf/tests/shell/test_brstack.sh +0 -0
index 85233d435be63..eb5837f82e390 100755
@@ -73,59 +79,88 @@ test_user_branches() {

[ ... ]

Quoted 1 line

test_kernel_branches() {

[ ... ]

tools/perf/tests/shell/test_brstack.sh test_user_branches() {

Quoted 7 lines
tools/perf/tests/shell/test_brstack.sh +2 -5
-		# Look for no source addresses without top bit set
-		if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
-			echo "ERROR: User branches found with kernel filter"
-			err=1
-		fi
+	# Look for no target addresses without top bit set
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{1,15}/" $TMPDIR/perf.script; then

Does the regex "{1,15}" incorrectly exclude single-digit hex addresses like "0x0"?

Since the commit message notes that Aarch64 may zero out user land addresses to prevent leaks, a zeroed address would be output as "0x0" by perf script.

The previous regex used "{0,15}", which would match "0x0", but the updated regex requires at least two hex digits when starting with 0-7.

Could this cause the test to silently miss zeroed addresses and produce a false negative?

Quoted 31 lines
tools/perf/tests/shell/test_brstack.sh +7 -21
+		echo "Testing kernel branch sampling [Failed user branches found]"
+		err=1
+	fi
+	if [ $err -eq 0 ]; then
+		echo "Testing kernel branch sampling [Passed]"
+		err=$start_err
 	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstack | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# Example of branch entries:
-		#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
-		# Source addresses come first and target address can be either
-		# userspace or kernel even with k option, as long as the source
-		# is in kernel.
-
-		#Look for source addresses with top bit set
-		if ! grep -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-			echo "ERROR: Kernel branches missing"
-			err=1
-		fi
-		# Look for no source addresses without top bit set
-		if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
-			echo "ERROR: User branches found with kernel filter"
-			err=1
-		fi
+		echo "Testing kernel branch sampling [Failed]"
 	fi
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v4 1/1
#11 lore raw
In reply to Ian Rogers
Quoted 17 lines · Ian Rogers

When filtering branch stack samples on user events they sample in user land but may have come from the kernel. Aarch64 avoids leaking the kernel address for kaslr reasons but other platforms, for now, don't. Be more permissive in allowing kernel addresses in the source of user branch stacks.

When filtering branch stack samples on kernel events they sample in kernel land but may have come from user land. Avoid the target being a user address but allow the source to be in user land. Aarch64 may not leak the user land addresses (making them 0) but other platforms do. As the kernel address sampling implies privelege, just allow this.

Increase the duration of the system call sampling test to make the likelihood of sampling a system call higher (increased from 1000 to 8000 loops - a number found through experimentation on an Intel Tigerlake laptop), also make the period of the event a prime number.

Quoted 8 lines

Put unneeded perf record output into a temporary file so that the test output isn't cluttered. More clearly state which test is running and the pass, fail or skipped result of the test.

These changes make the test on an Intel tigerlake laptop reliably pass rather than reliably fail.

Quoted 226 lines

---
v4: Address feedback from James Clark.
v2,v3: Address Sashiko feedback.

---
  tools/perf/tests/shell/test_brstack.sh | 146 ++++++++++++++++---------
  1 file changed, 96 insertions(+), 50 deletions(-)
tools/perf/tests/shell/test_brstack.sh +96 -50
index 85233d435be6..eb5837f82e39 100755
@@ -38,9 +38,13 @@ is_arm64() {
  	[ "$(uname -m)" = "aarch64" ];
  }
 
+has_kaslr_bug() {
+	[ "$(uname -m)" != "aarch64" ];
+}
+
  check_branches() {
  	if ! tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep -E -m1 -q "$1"; then
-		echo "Branches missing $1"
+		echo "ERROR: Branches missing $1"
  		err=1
  	fi
  }
@@ -48,6 +52,8 @@ check_branches() {
  test_user_branches() {
  	echo "Testing user branch stack sampling"
 
+	start_err=$err
+	err=0
  	perf record -o "$TMPDIR/perf.data" --branch-filter any,save_type,u -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
  	perf script -i "$TMPDIR/perf.data" --fields brstacksym > "$TMPDIR/perf.script"
 
@@ -73,59 +79,88 @@ test_user_branches() {
  	perf script -i "$TMPDIR/perf.data" --fields brstack | \
  		tr ' ' '\n' > "$TMPDIR/perf.script"
 
-	# There should be no kernel addresses with the u option, in either
-	# source or target addresses.
-	if grep -E -m1 "0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-		echo "ERROR: Kernel address found in user mode"
+	# There should be no kernel addresses in the target with the u option.
+	local regex="0x[89a-f][0-9a-f]{15}"
+	if has_kaslr_bug; then
+		# If the system has a kaslr bug that may leak kernel addresses
+		# in the source of something like an ERET/SYSRET. Make the regex
+		# more specific and just check the target address is in user
+		# code.
+		regex="^0x[0-9a-f]{0,16}/0x[89a-f][0-9a-f]{15}/"
+	fi
+	if grep -q -E -m1 "$regex" $TMPDIR/perf.script; then
+		echo "Testing user branch stack sampling [Failed kernel address found in user mode]"
  		err=1
  	fi
  	# some branch types are still not being tested:
  	# IND COND_CALL COND_RET SYSRET SERROR NO_TX
+	if [ $err -eq 0 ]; then
+		echo "Testing user branch stack sampling [Passed]"
+		err=$start_err
+	else
+		echo "Testing user branch stack sampling [Failed]"
+	fi
  }
 
  test_trap_eret_branches() {
  	echo "Testing trap & eret branches"
+
  	if ! is_arm64; then
-		echo "skip: not arm64"
+		echo "Testing trap & eret branches [Skipped not arm64]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
+		perf test -w traploop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
+	check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
+	check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+	if [ $err -eq 0 ]; then
+		echo "Testing trap & eret branches [Passed]"
+		err=$start_err
  	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
-			perf test -w traploop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
-		check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
-		check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+		echo "Testing trap & eret branches [Failed]"
  	fi
  }
 
  test_kernel_branches() {
-	echo "Testing that k option only includes kernel source addresses"
+	echo "Testing kernel branch sampling"
 
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing that k option [Skipped not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
+		perf bench syscall basic --loop 1000 > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstack | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	# Example of branch entries:
+	#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
+	# Source addresses come first in user or kernel code. Next is the target
+	# address that must be in the kernel.
+
+	# Look for source addresses with top bit set
+	if ! grep -q -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
+		echo "Testing kernel branch sampling [Failed kernel branches missing]"
+		err=1
+	fi
+	# Look for no target addresses without top bit set
+	if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{1,15}/" $TMPDIR/perf.script; then
+		echo "Testing kernel branch sampling [Failed user branches found]"
+		err=1
+	fi
+	if [ $err -eq 0 ]; then
+		echo "Testing kernel branch sampling [Passed]"
+		err=$start_err
  	else
-		perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstack | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		# Example of branch entries:
-		#       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
-		# Source addresses come first and target address can be either
-		# userspace or kernel even with k option, as long as the source
-		# is in kernel.
-
-		#Look for source addresses with top bit set
-		if ! grep -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-			echo "ERROR: Kernel branches missing"
-			err=1
-		fi
-		# Look for no source addresses without top bit set
-		if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
-			echo "ERROR: User branches found with kernel filter"
-			err=1
-		fi
+		echo "Testing kernel branch sampling [Failed]"
  	fi
  }
 
@@ -136,14 +171,15 @@ test_filter() {
  	test_filter_expect=$2
 
  	echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
-	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- ${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
+	perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- \
+		${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
  	perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.script"
 
  	# fail if we find any branch type that doesn't match any of the expected ones
  	# also consider UNKNOWN branch types (-)
  	if [ ! -s "$TMPDIR/perf.script" ]
  	then
-		echo "Empty script output"
+		echo "Testing branch stack filtering [Failed empty script output]"
  		err=1
  		return
  	fi
@@ -154,26 +190,36 @@ test_filter() {
  	  > "$TMPDIR/perf.script-filtered" || true
  	if [ -s "$TMPDIR/perf.script-filtered" ]
  	then
-		echo "Unexpected branch filter in script output"
+		echo "Testing branch stack filtering [Failed unexpected branch filter]"
  		cat "$TMPDIR/perf.script"
  		err=1
  		return
  	fi
+	echo "Testing branch stack filtering [Passed]"
  }
 
  test_syscall() {
  	echo "Testing syscalls"
  	# skip if perf doesn't have enough privileges
-	if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-		echo "skip: not enough privileges"
+	if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+		echo "Testing syscalls [Skipped: not enough privileges]"
+		return
+	fi
+	start_err=$err
+	err=0
+	perf record -o $TMPDIR/perf.data --branch-filter \
+		any_call,save_type,u,k -c 10007 -- \
+		perf bench syscall basic --loop 8000  > "$TMPDIR/record.txt" 2>&1
+	perf script -i $TMPDIR/perf.data --fields brstacksym | \
+		tr ' ' '\n' > $TMPDIR/perf.script
+
+	check_branches "getppid[^ ]*/SYSCALL/"
+
+	if [ $err -eq 0 ]; then
+		echo "Testing syscalls [Passed]"
+		err=$start_err
  	else
-		perf record -o $TMPDIR/perf.data --branch-filter \
-			any_call,save_type,u,k -c 10000 -- \
-			perf bench syscall basic --loop 1000
-		perf script -i $TMPDIR/perf.data --fields brstacksym | \
-			tr ' ' '\n' > $TMPDIR/perf.script
-
-		check_branches "getppid[^ ]*/SYSCALL/"
+		echo "Testing syscalls [Failed]"
  	fi
  }
  set -e
author reviewer
#12 lore raw
Quoted 25 lines · James Clark
Quoted 21 lines · Ian Rogers

When filtering branch stack samples on user events they sample in user land but may have come from the kernel. Aarch64 avoids leaking the kernel address for kaslr reasons but other platforms, for now, don't. Be more permissive in allowing kernel addresses in the source of user branch stacks.

When filtering branch stack samples on kernel events they sample in kernel land but may have come from user land. Avoid the target being a user address but allow the source to be in user land. Aarch64 may not leak the user land addresses (making them 0) but other platforms do. As the kernel address sampling implies privelege, just allow this.

Increase the duration of the system call sampling test to make the likelihood of sampling a system call higher (increased from 1000 to 8000 loops - a number found through experimentation on an Intel Tigerlake laptop), also make the period of the event a prime number.

Put unneeded perf record output into a temporary file so that the test output isn't cluttered. More clearly state which test is running and the pass, fail or skipped result of the test.

Quoted 6 lines
Quoted 4 lines

These changes make the test on an Intel tigerlake laptop reliably pass rather than reliably fail.

Thanks James, hopefully this will be included in the merge window.

Ian

Quoted 228 lines
Quoted 226 lines

---
v4: Address feedback from James Clark.
v2,v3: Address Sashiko feedback.

---
  tools/perf/tests/shell/test_brstack.sh | 146 ++++++++++++++++---------
  1 file changed, 96 insertions(+), 50 deletions(-)
tools/perf/tests/shell/test_brstack.sh +96 -50
index 85233d435be6..eb5837f82e39 100755
@@ -38,9 +38,13 @@ is_arm64() {
      [ "$(uname -m)" = "aarch64" ];
  }
 
+has_kaslr_bug() {
+     [ "$(uname -m)" != "aarch64" ];
+}
+
  check_branches() {
      if ! tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep -E -m1 -q "$1"; then
-             echo "Branches missing $1"
+             echo "ERROR: Branches missing $1"
              err=1
      fi
  }
@@ -48,6 +52,8 @@ check_branches() {
  test_user_branches() {
      echo "Testing user branch stack sampling"
 
+     start_err=$err
+     err=0
      perf record -o "$TMPDIR/perf.data" --branch-filter any,save_type,u -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
      perf script -i "$TMPDIR/perf.data" --fields brstacksym > "$TMPDIR/perf.script"
 
@@ -73,59 +79,88 @@ test_user_branches() {
      perf script -i "$TMPDIR/perf.data" --fields brstack | \
              tr ' ' '\n' > "$TMPDIR/perf.script"
 
-     # There should be no kernel addresses with the u option, in either
-     # source or target addresses.
-     if grep -E -m1 "0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-             echo "ERROR: Kernel address found in user mode"
+     # There should be no kernel addresses in the target with the u option.
+     local regex="0x[89a-f][0-9a-f]{15}"
+     if has_kaslr_bug; then
+             # If the system has a kaslr bug that may leak kernel addresses
+             # in the source of something like an ERET/SYSRET. Make the regex
+             # more specific and just check the target address is in user
+             # code.
+             regex="^0x[0-9a-f]{0,16}/0x[89a-f][0-9a-f]{15}/"
+     fi
+     if grep -q -E -m1 "$regex" $TMPDIR/perf.script; then
+             echo "Testing user branch stack sampling [Failed kernel address found in user mode]"
              err=1
      fi
      # some branch types are still not being tested:
      # IND COND_CALL COND_RET SYSRET SERROR NO_TX
+     if [ $err -eq 0 ]; then
+             echo "Testing user branch stack sampling [Passed]"
+             err=$start_err
+     else
+             echo "Testing user branch stack sampling [Failed]"
+     fi
  }
 
  test_trap_eret_branches() {
      echo "Testing trap & eret branches"
+
      if ! is_arm64; then
-             echo "skip: not arm64"
+             echo "Testing trap & eret branches [Skipped not arm64]"
+             return
+     fi
+     start_err=$err
+     err=0
+     perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
+             perf test -w traploop 1000 > "$TMPDIR/record.txt" 2>&1
+     perf script -i $TMPDIR/perf.data --fields brstacksym | \
+             tr ' ' '\n' > $TMPDIR/perf.script
+
+     # BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
+     check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
+     check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+     if [ $err -eq 0 ]; then
+             echo "Testing trap & eret branches [Passed]"
+             err=$start_err
      else
-             perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
-                     perf test -w traploop 1000
-             perf script -i $TMPDIR/perf.data --fields brstacksym | \
-                     tr ' ' '\n' > $TMPDIR/perf.script
-
-             # BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
-             check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
-             check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
+             echo "Testing trap & eret branches [Failed]"
      fi
  }
 
  test_kernel_branches() {
-     echo "Testing that k option only includes kernel source addresses"
+     echo "Testing kernel branch sampling"
 
-     if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-             echo "skip: not enough privileges"
+     if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+             echo "Testing that k option [Skipped not enough privileges]"
+             return
+     fi
+     start_err=$err
+     err=0
+     perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
+             perf bench syscall basic --loop 1000 > "$TMPDIR/record.txt" 2>&1
+     perf script -i $TMPDIR/perf.data --fields brstack | \
+             tr ' ' '\n' > $TMPDIR/perf.script
+
+     # Example of branch entries:
+     #       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
+     # Source addresses come first in user or kernel code. Next is the target
+     # address that must be in the kernel.
+
+     # Look for source addresses with top bit set
+     if ! grep -q -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
+             echo "Testing kernel branch sampling [Failed kernel branches missing]"
+             err=1
+     fi
+     # Look for no target addresses without top bit set
+     if grep -q -E -m1 "^0x[0-9a-f]{0,16}/0x[0-7][0-9a-f]{1,15}/" $TMPDIR/perf.script; then
+             echo "Testing kernel branch sampling [Failed user branches found]"
+             err=1
+     fi
+     if [ $err -eq 0 ]; then
+             echo "Testing kernel branch sampling [Passed]"
+             err=$start_err
      else
-             perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
-                     perf bench syscall basic --loop 1000
-             perf script -i $TMPDIR/perf.data --fields brstack | \
-                     tr ' ' '\n' > $TMPDIR/perf.script
-
-             # Example of branch entries:
-             #       "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
-             # Source addresses come first and target address can be either
-             # userspace or kernel even with k option, as long as the source
-             # is in kernel.
-
-             #Look for source addresses with top bit set
-             if ! grep -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
-                     echo "ERROR: Kernel branches missing"
-                     err=1
-             fi
-             # Look for no source addresses without top bit set
-             if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
-                     echo "ERROR: User branches found with kernel filter"
-                     err=1
-             fi
+             echo "Testing kernel branch sampling [Failed]"
      fi
  }
 
@@ -136,14 +171,15 @@ test_filter() {
      test_filter_expect=$2
 
      echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
-     perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- ${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
+     perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- \
+             ${TESTPROG}  > "$TMPDIR/record.txt" 2>&1
      perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.script"
 
      # fail if we find any branch type that doesn't match any of the expected ones
      # also consider UNKNOWN branch types (-)
      if [ ! -s "$TMPDIR/perf.script" ]
      then
-             echo "Empty script output"
+             echo "Testing branch stack filtering [Failed empty script output]"
              err=1
              return
      fi
@@ -154,26 +190,36 @@ test_filter() {
        > "$TMPDIR/perf.script-filtered" || true
      if [ -s "$TMPDIR/perf.script-filtered" ]
      then
-             echo "Unexpected branch filter in script output"
+             echo "Testing branch stack filtering [Failed unexpected branch filter]"
              cat "$TMPDIR/perf.script"
              err=1
              return
      fi
+     echo "Testing branch stack filtering [Passed]"
  }
 
  test_syscall() {
      echo "Testing syscalls"
      # skip if perf doesn't have enough privileges
-     if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
-             echo "skip: not enough privileges"
+     if ! perf record --branch-filter any,k -o- -- true > "$TMPDIR/record.txt" 2>&1; then
+             echo "Testing syscalls [Skipped: not enough privileges]"
+             return
+     fi
+     start_err=$err
+     err=0
+     perf record -o $TMPDIR/perf.data --branch-filter \
+             any_call,save_type,u,k -c 10007 -- \
+             perf bench syscall basic --loop 8000  > "$TMPDIR/record.txt" 2>&1
+     perf script -i $TMPDIR/perf.data --fields brstacksym | \
+             tr ' ' '\n' > $TMPDIR/perf.script
+
+     check_branches "getppid[^ ]*/SYSCALL/"
+
+     if [ $err -eq 0 ]; then
+             echo "Testing syscalls [Passed]"
+             err=$start_err
      else
-             perf record -o $TMPDIR/perf.data --branch-filter \
-                     any_call,save_type,u,k -c 10000 -- \
-                     perf bench syscall basic --loop 1000
-             perf script -i $TMPDIR/perf.data --fields brstacksym | \
-                     tr ' ' '\n' > $TMPDIR/perf.script
-
-             check_branches "getppid[^ ]*/SYSCALL/"
+             echo "Testing syscalls [Failed]"
      fi
  }
  set -e
maintainer Applied
#13 lore raw
In reply to Ian Rogers
Quoted 6 lines · Ian Rogers

When filtering branch stack samples on user events they sample in user land but may have come from the kernel. Aarch64 avoids leaking the kernel address for kaslr reasons but other platforms, for now, don't. Be more permissive in allowing kernel addresses in the source of user branch stacks.

Quoted 7 lines

When filtering branch stack samples on kernel events they sample in kernel land but may have come from user land. Avoid the target being a user address but allow the source to be in user land. Aarch64 may not leak the user land addresses (making them 0) but other platforms do. As the kernel address sampling implies privelege, just allow this.

[...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung