From nobody Mon Jun 15 05:25:31 2026 Received: from mail-dl1-f74.google.com (mail-dl1-f74.google.com [74.125.82.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDEE236AB77 for ; Wed, 8 Apr 2026 06:58:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775631528; cv=none; b=IxbqLObCqqjvNZrnD1x4dwHZGKmEOV0LSLSUM85AtzI5CJfDQ6tDUQD4P5ZXkhXlXxhIbzQ67Yy1484a+u2nihoqCDFH0vSdS8r1/xgq9zaqIeby060RLTNOYyOl/blA1Fbygp9srbsHZbGvkTUmvhmXFIzYSERYl+7+hIHRwvw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775631528; c=relaxed/simple; bh=Nn0miRX6O32JQOtPVCr92wiRbgVDw32950L1rvjPbWw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=Kb2rT9fku9xopUiZtP5fzrx5kAXNtpr08AcpT+XwNU6ySK8EOxbkLPB87P0WYAyyaQwwAVbZ6xvjvtUxq9g9zD6FBqdFdnz9j7vKE04ts0vU1jU73Aa3n/Pefvgbplbdru8VW/vRH4orM86t+y2E4HQDvx7y+CX87qkLll9K4iw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nYupg3A7; arc=none smtp.client-ip=74.125.82.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nYupg3A7" Received: by mail-dl1-f74.google.com with SMTP id a92af1059eb24-12737f276a2so9261833c88.1 for ; Tue, 07 Apr 2026 23:58:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775631525; x=1776236325; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=fLtFtC/oi2eHemCIy4gg+BvRUETIN+tHfDnRPBDklJg=; b=nYupg3A7BU8YpZRdiaSdNmnFExr0ZOct+A4DlzmguRQ+Bi2Aa5GMpgEyKkz23V5SPk Bg23/wFlF71NW17bx253BiIvs0xYeeIvQEGKviu1k7QfjFCp02WweNh3j58ey79Wvu7j SugNrX3Y8mEm36yl/mRdrdKPFR3lYiGRjfiEMalW1BpwVdXLUeW3pHO14jNoFtTffn29 rj59UNCZjm49tuXxT0M4o5u1aVnV03sNadeD+O390NpOGhaa8p/830DhBRBHtCNpwuKN Q8vpAIN2l6ThfYfkv+2+1OaigBJyOdkJDcGOy5VFhW5Cpw+0PUMiRNEEebkvOTplpfaG d7qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775631525; x=1776236325; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fLtFtC/oi2eHemCIy4gg+BvRUETIN+tHfDnRPBDklJg=; b=VkbkkalUW1vhGCopuWwGNQYmXPheVDpBjX6bi39Gy0533IIMDAFQxhwFn35sge1t9e 3t3Sayq8Nr3zvpJ8dDN8mT7oVAc6I/V0lCdCuBy1Y3FBwChtdORCIDlhk0tuj44+PYkB ngd4I8hg88IMOS4/ZsuwLBJhjZMBXC6Ll8l5Ne2zPNHmJuaL6bYd3q3lO9UOoYwz9v+x dkGJgamBFREuC8VOzvQJ4Zv/s8bhGoe9aYyQ9MhdMEMA416Q5aJa1fN0ysissLSOTH/A b8ve3FyWeJVSubYoBvKb5/kPO/TXpGGpQtLLRPLh/mH4Le49eZB7LI4nnZ+pbng2r+KW vWCg== X-Forwarded-Encrypted: i=1; AJvYcCUSNuPU1pjdthwrhzniAua4v2TW6SeZ+ngVDJCQ/D3lbFa2wYgUAiFWG3ORBHt4RSCsgn8Ghhll5t4Ew18=@vger.kernel.org X-Gm-Message-State: AOJu0Yymdq9ZS1yGwrEz924XtJA1f9wa1xo3+eSrWSREZ+WBMKW/d2OB 4tKXLZgyDjjbXVsMU1vME611M0YkdSrWXvXCknZKXLP61PqIc1lQ3F+dKMF3uuAICEz+JxcmvB1 cbe7P8r0mhQ== X-Received: from dlbvg4.prod.google.com ([2002:a05:7022:7f04:b0:12c:18c1:703c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:ec17:b0:12b:ebb8:e518 with SMTP id a92af1059eb24-12bfb779d53mr9218106c88.35.1775631524823; Tue, 07 Apr 2026 23:58:44 -0700 (PDT) Date: Tue, 7 Apr 2026 23:58:41 -0700 In-Reply-To: <20260408062048.61252-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260408062048.61252-1-irogers@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260408065841.165393-1-irogers@google.com> Subject: [PATCH v3] perf test: Fixes for check branch stack sampling From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Ian Rogers --- tools/perf/tests/shell/test_brstack.sh | 134 ++++++++++++++++--------- 1 file changed, 84 insertions(+), 50 deletions(-) diff --git a/tools/perf/tests/shell/test_brstack.sh b/tools/perf/tests/shel= l/test_brstack.sh index 85233d435be6..025ed9d3d110 100755 --- a/tools/perf/tests/shell/test_brstack.sh +++ b/tools/perf/tests/shell/test_brstack.sh @@ -40,7 +40,7 @@ is_arm64() { =20 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=3D1 fi } @@ -48,6 +48,8 @@ check_branches() { test_user_branches() { echo "Testing user branch stack sampling" =20 + start_err=3D$err + err=3D0 perf record -o "$TMPDIR/perf.data" --branch-filter any,save_type,u -- ${T= ESTPROG} > "$TMPDIR/record.txt" 2>&1 perf script -i "$TMPDIR/perf.data" --fields brstacksym > "$TMPDIR/perf.sc= ript" =20 @@ -73,59 +75,80 @@ test_user_branches() { perf script -i "$TMPDIR/perf.data" --fields brstack | \ tr ' ' '\n' > "$TMPDIR/perf.script" =20 - # 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=3D1 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=3D$start_err + else + echo "Testing user branch stack sampling [Failed]" + fi } =20 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=3D$err + err=3D0 + 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.TYPE =3D=3D 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=3D$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.TYPE =3D=3D 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 } =20 test_kernel_branches() { - echo "Testing that k option only includes kernel source addresses" + echo "Testing kernel branch sampling" =20 - 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=3D$err + err=3D0 + 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=3D1 + 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=3D1 + fi + if [ $err -eq 0 ]; then + echo "Testing kernel branch sampling [Passed]" + err=3D$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=3D1 - 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=3D1 - fi + echo "Testing kernel branch sampling [Failed]" fi } =20 @@ -136,14 +159,15 @@ test_filter() { test_filter_expect=3D$2 =20 echo "Testing branch stack filtering permutation ($test_filter_filter,$te= st_filter_expect)" - perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,s= ave_type,u" -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1 + perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,s= ave_type,u" -- \ + ${TESTPROG} > "$TMPDIR/record.txt" 2>&1 perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.scrip= t" =20 # 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=3D1 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=3D1 return fi + echo "Testing branch stack filtering [Passed]" } =20 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=3D$err + err=3D0 + 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=3D$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 --=20 2.53.0.1213.gd9a14994de-goog