]> www.infradead.org Git - users/hch/misc.git/commitdiff
perf test: Add syscall and address tests to brstack test
authorJames Clark <james.clark@linaro.org>
Wed, 13 Aug 2025 13:38:50 +0000 (14:38 +0100)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 1 Oct 2025 18:31:34 +0000 (15:31 -0300)
Test that SYSCALL type branches are emitted from the expected 'getppid'
symbol. Test that when only 'k' is used, sources addresses are all in
the kernel. Test that no kernel addresses leak by checking for them in
the 'u' test.

Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: James Clark <james.clark@linaro.org>
Cc: Adam Young <admiyo@os.amperecomputing.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Anshuman Khandual <anshuman.khandual@arm.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rob Herring <robh@kernel.org>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/shell/test_brstack.sh

index 46e5aa9ce8ee7b22158a43487fc94e9fac91bf7c..252d22d39c7b0ac2eea25583abba10032697f761 100755 (executable)
@@ -64,8 +64,50 @@ test_user_branches() {
        do
                check_branches "$x"
        done
+
+       # Dump addresses only this time
+       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"
+               err=1
+       fi
        # some branch types are still not being tested:
-       # IND COND_CALL COND_RET SYSCALL SYSRET IRQ SERROR NO_TX
+       # IND COND_CALL COND_RET SYSRET IRQ SERROR NO_TX
+}
+
+
+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"
+       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
+       fi
 }
 
 # first argument <arg0> is the argument passed to "--branch-stack <arg0>,save_type,u"
@@ -100,9 +142,26 @@ test_filter() {
        fi
 }
 
+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"
+       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/"
+       fi
+}
 set -e
 
 test_user_branches
+test_syscall
+test_kernel_branches
 
 any_call="CALL|IND_CALL|COND_CALL|SYSCALL|IRQ"