Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found
@ 2026-11-17 20:04 Arnaldo Carvalho de Melo
  2026-11-17 20:04 ` [PATCH v6 01/13] perf jvmti: Link the agent with EXTRA_CFLAGS Arnaldo Carvalho de Melo
                   ` (12 more replies)
  0 siblings, 13 replies; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

Hi,

Nothing measured how much of perf the test suite reaches.  Patch 1 links
the jvmti agent with EXTRA_CFLAGS, without which the coverage build
never saw its counters; patch 2 adds tools/perf/scripts/coverage.sh: it
builds perf with clang source based coverage, runs the complete perf
test suite and reports line and function coverage, optionally against a
baseline, with --table and --html like pahole's coverage_table.py.  Also
as 'make -C tools/perf coverage', 'coverage-prep' and 'coverage-table'.

The useful output is the list of files with the most uncovered lines,
computed over the whole tree and not only the files the tests already
touch: that is where new tests should try to get more coverage.

The rest is what the first runs found, in the code, the build and the
tests:

  - The data type resolver missed 32-bit absolute addressing (patch 3);
    the DW_OP_addrx globals it also missed are fixed upstream by
    c3991e42f58a3650, so patch 4 only adds the test.  A struct pointer
    kept in a register at -O2 had no test (patch 7).
  - The flexible array member had no test; one showed the struct row at
    0.00% while the member said 100.00% (patches 5-6).
  - --insn-stat was printed before the loop that accounts the samples,
    so --code-with-type showed zeros (patch 8).
  - The libbfd probe printed a missing bfd.h error five times per
    build-test run (patch 9).
  - FEATURE-DUMP didn't record llvm-perf and rust, disabling both in the
    build-test builds (patch 10).
  - The Intel PT virtual LBR check lost every aux sample with a single
    data page (patch 11).
  - The dlfilter test kept samples whose IP the kernel now zeroes on a
    userspace-to-kernel skid (patch 12).
  - The LBR inline unwinding check failed instead of reporting Skip when
    the AMD PMU rejects branch sampling (patch 13).

With coverage-prep by the developer (clang DWARF 5) and the full suite
as root on this AMD host, 105,466 of 205,823 lines were covered
(51.24%; v5: 50.67% of 205,810), 86,344 of 173,257 in tools/perf/
(49.84%). 168 tests passed, 28 skipped and one failed (20.2, the
POLL_HUP regression, outside this series). The global variable and
instruction-stats tests passed; the Intel PT hardware tests skipped,
so patch 11's 64-page AUX buffer has not yet been tested on Intel.
Patch 2's changelog excerpt shows an earlier root run.

The harness and the tests were developed with an AI coding assistant
(OpenCode), which drafted the script and the tests and helped debug the
failures; an independent local model reviewed the script and I verified
its findings before folding them in.  I reviewed every change and
reproduced each failure before fixing it.

What changed from v5:

  - Patch 1 is new (jvmti link); the harness is 2, 3-13 renumber, and 2
    folds the sashiko-bot and local review fixes; 3-8 and 11 carry the
    data type, stats and per-thread fixes; 13 skips only EOPNOTSUPP now,
    reversing v3.  Ian's tags: 05-07, 09 and 12 (05-07 test-only, object
    if unwanted); 02-04, 08 and 13 unreviewed.

What changed from v4:

  - Rebased onto perf-tools-next b7ff10b9 (52 commits).
  - Patch 3: the DW_OP_addrx fix is upstream as c3991e42f58a3650 ("perf
    dwarf-aux: Support DW_OP_addrx locations for global variables"), by
    James Clark, so the patch is now test-only.
  - Patch 12: also match "doesn't support branch stack sampling", how
    evsel__open_strerror() formats EOPNOTSUPP, so the test skips on AMD
    BRS (sashiko-bot).
  - Ian Rogers' Reviewed-by on v3 is carried on patches 4-6, 8 and 10-11;
    patches 1, 3 and 12 changed after that review and need a new look.

What changed from v3:

  - Patch 1: a .profraw left half written used to make llvm-profdata drop
    the whole merge, and with it the run's measurement; the bad files are
    now skipped and counted (found running the harness on the arm64 boards).
  - Patch 2: only set mem_ref for bare number operands when the offset is
    non-zero, and guard find_data_type against reg1 == -1 (sashiko-bot).
  - Patch 3: move DW_OP_addrx fallback to dwarf-aux.h so annotate-data.c
    builds with older elfutils, and fix typo in Fixes SHA (sashiko-bot).
  - Patch 12: only "Invalid argument"/"not supported" skips (exit 2);
    stderr goes to a file, any other record failure prints it and fails.
  - Ian Rogers' Reviewed-by on v3 is carried on patches 4-6, 8 and
    10-11; patches 1 and 12 changed after that review and need a new look.

What changed from v2:

  - Patch 2 is new: absolute addressing is resolved, so the new data type
    tests pass on 32-bit non-PIE builds and skip on 32-bit PIE.
  - Patches 3, 4 and 6: the C programs became 'perf test -w' workloads
    (global_data, flex_array, regptr), the shell tests no longer compile
    anything (Ian Rogers).
  - Patch 6: regptr uses long long members, so the offsets are the same
    on ILP32 and LP64.
  - Patch 5: add the missing annotated_member__extent() stub, so a
    build with slang but without libdw works (sashiko-bot); it is now
    a method of struct annotated_member.

What changed from v1:

  - Patch 9 (cs-etm) is gone: Leo Yan's series replaces it.
  - Patch 1: O= is accepted by all targets, the Python extension is in
    the report, -f is normalized and the delta matches its baseline.
  - Patch 5: the TUI browser counts flexible array accesses too.
  - Patch 7: the stats test skips without libdw.
  - Patches 10 and 12: Fixes tags corrected.

Best regards,

- Arnaldo

Arnaldo Carvalho de Melo (13):
  perf jvmti: Link the agent with EXTRA_CFLAGS
  perf scripts: Add a coverage harness for perf
  perf annotate: Handle absolute addressing in data type profiling
  perf tests: Exercise DW_OP_addrx global variables
  perf tests: Exercise the flexible array member path
  perf annotate-data: Count flexible array accesses for the whole struct
  perf tests: Exercise data type resolution from a register
  perf annotate: Print the stats after the annotations
  perf tests: Silence the libbfd probe in build-test
  perf build: Record the display-only features in FEATURE-DUMP
  perf tests: Fix the Intel PT virtual LBR check
  perf test: Drop samples without an IP in the dlfilter test
  perf test: Skip the LBR inline unwinding check when unsupported

 tools/perf/.gitignore                         |   1 +
 tools/perf/Makefile                           |  11 +-
 tools/perf/Makefile.config                    |   4 +
 tools/perf/Makefile.perf                      |   6 +-
 tools/perf/builtin-annotate.c                 |  19 +-
 tools/perf/scripts/coverage.sh                | 984 ++++++++++++++++++
 tools/perf/tests/builtin-test.c               |   3 +
 tools/perf/tests/make                         |   2 +-
 tools/perf/tests/shell/addr2line_inlines.sh   |  23 +-
 tools/perf/tests/shell/annotate_stats.sh      |  69 ++
 .../perf/tests/shell/data_type_flex_array.sh  | 108 ++
 tools/perf/tests/shell/data_type_global.sh    |  68 ++
 tools/perf/tests/shell/data_type_register.sh  |  60 ++
 tools/perf/tests/shell/script_dlfilter.sh     |   2 +-
 tools/perf/tests/shell/test_intel_pt.sh       |   9 +-
 tools/perf/tests/tests.h                      |   3 +
 tools/perf/tests/workloads/Build              |   6 +
 tools/perf/tests/workloads/flex_array.c       |  55 +
 tools/perf/tests/workloads/global_data.c      |  55 +
 tools/perf/tests/workloads/regptr.c           |  61 ++
 tools/perf/ui/browsers/annotate-data.c        |   7 +-
 tools/perf/util/annotate-arch/annotate-x86.c  |  52 +-
 tools/perf/util/annotate-data.c               |  25 +-
 tools/perf/util/annotate-data.h               |   8 +
 tools/perf/util/annotate.c                    |  25 +-
 tools/perf/util/annotate.h                    |   7 +
 26 files changed, 1648 insertions(+), 25 deletions(-)
 create mode 100755 tools/perf/scripts/coverage.sh
 create mode 100755 tools/perf/tests/shell/annotate_stats.sh
 create mode 100755 tools/perf/tests/shell/data_type_flex_array.sh
 create mode 100755 tools/perf/tests/shell/data_type_global.sh
 create mode 100755 tools/perf/tests/shell/data_type_register.sh
 create mode 100644 tools/perf/tests/workloads/flex_array.c
 create mode 100644 tools/perf/tests/workloads/global_data.c
 create mode 100644 tools/perf/tests/workloads/regptr.c


base-commit: b7ff10b9ddec8da75304c40fe4abf1ee4f9f0a43
v1-head: 3f5465bea8f5a97a
v2-head: 9706c488afffeb6d
v3-head: f2daf1586528671a
v4-head: b9e240a1b80ecd2a
v5-head: a21454e3d055c93b
v6-head: e1315408a335617d
-- 
2.56.0


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v6 01/13] perf jvmti: Link the agent with EXTRA_CFLAGS
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:10   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 02/13] perf scripts: Add a coverage harness for perf Arnaldo Carvalho de Melo
                   ` (11 subsequent siblings)
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

The jvmti objects are compiled with EXTRA_CFLAGS but the agent is linked
with LDFLAGS only, so flags with link-time parts never reach the shared
object.  With -fprofile-instr-generate the agent's __profc counters had
no __llvm_profile runtime to write them, and its files showed 0.00% in
the coverage report.

Pass EXTRA_CFLAGS at link time, as the dlfilter rule already does.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/Makefile.perf | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index b74b534e1153ffcb..9f3e248d5c4efff8 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -791,7 +791,7 @@ $(LIBJVMTI_IN): prepare FORCE
 	$(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=jvmti obj=jvmti
 
 $(OUTPUT)$(LIBJVMTI): $(LIBJVMTI_IN)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -shared -Wl,-soname -Wl,$(LIBJVMTI) -o $@ $<
+	$(QUIET_LINK)$(CC) $(LDFLAGS) $(EXTRA_CFLAGS) -shared -Wl,-soname -Wl,$(LIBJVMTI) -o $@ $<
 endif
 
 $(patsubst perf-%,%.o,$(PROGRAMS)): $(wildcard */*.h)
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 02/13] perf scripts: Add a coverage harness for perf
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
  2026-11-17 20:04 ` [PATCH v6 01/13] perf jvmti: Link the agent with EXTRA_CFLAGS Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:15   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 03/13] perf annotate: Handle absolute addressing in data type profiling Arnaldo Carvalho de Melo
                   ` (10 subsequent siblings)
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

Build perf with clang source based coverage, run the complete perf test
suite, merge the raw profiles and report line and function coverage,
optionally against a baseline.  This is pahole's coverage/coverage.sh +
coverage-diff.sh merged into one script, wired as
'make -C tools/perf coverage' and listed in 'make help'.

Without -f the report lists the files with the most uncovered lines,
which is where new tests should try to get more coverage; -t narrows
the tests, -f focuses on specific files, --html writes a browsable
report and --table/-T, also available as 'make -C tools/perf
coverage-table', prints the last run as an ASCII table with the
per-file line and function coverage, like pahole's coverage_table.py.

The build can be split from the test run: 'make -C tools/perf
coverage-prep' builds the instrumented perf and records the build
directory, and a later run as root, which needs no make, checks that
build, runs the tests and renders the report, or prints the table with
--table.  This is for running the tests that need root on the bare
metal host while the instrumented perf is built in the development
container.

The report covers the whole tree, not only the paths the tests walk, so
the per-file table is a map of what the suite does not reach in a given
run.  The uncovered line list and the failures the runs exposed are what
drove the rest of this series.

  $ make -C tools/perf coverage-prep O=~/coverage-build
  ...
  $ sudo scripts/coverage.sh
  ...
  Total: 104787 of 205810 lines covered (50.91%)
    perf: 85674 of 173247 lines covered (49.45%)

    largest uncovered files (>= 100 lines)
    file                                            lines     cover    missed  fn-cover
    util/intel-pt.c                                  3237     0.00%      3237     0.00%
    util/intel-pt-decoder/intel-pt-decoder.c         3208     0.00%      3208     0.00%
    ui/browsers/hists.c                              2730     0.00%      2730     0.00%
    builtin-sched.c                                  3737    41.26%      2195    43.97%
    builtin-script.c                                 3212    33.50%      2136    37.90%
    util/auxtrace.c                                  2013     4.47%      1923    11.11%
    util/session.c                                   2686    41.14%      1581    48.80%
    ...
  Report:  /home/acme/coverage-build/cov/report.txt

The total also counts the tools/ libraries and headers linked into
perf, the perf line is the tools/perf/ files alone.  The numbers above
are from the split run: coverage-prep built the instrumented perf as the
developer, the tests and the report ran as root.  This machine has no
Intel PT, so the PT tests skip and leave intel-pt.c and
intel-pt-decoder.c at 0.00%; the TUI browser is in the list because the
suite does not drive it.

'perf test' exits 0 even when tests fail or time out, so the script
takes its status from the "Failed tests :" and "Timed out tests :" lines
that perf test prints since commit 33f20342ba525ef7 ("perf test: Add
summary reporting").

168 tests passed, 28 skipped, one failure (20.2: Tracepoint event), in
code this series does not change.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/.gitignore          |   1 +
 tools/perf/Makefile            |  11 +-
 tools/perf/Makefile.perf       |   4 +
 tools/perf/scripts/coverage.sh | 984 +++++++++++++++++++++++++++++++++
 4 files changed, 999 insertions(+), 1 deletion(-)
 create mode 100755 tools/perf/scripts/coverage.sh

diff --git a/tools/perf/.gitignore b/tools/perf/.gitignore
index 3b968c5158b822ce..9052f25597ac9489 100644
--- a/tools/perf/.gitignore
+++ b/tools/perf/.gitignore
@@ -27,6 +27,7 @@ stats-*.csv
 cscope*
 config.mak
 config.mak.autogen
+.coverage-build-dir
 *-bison.*
 *-flex.*
 *.pyc
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index 00baf90e5bda7123..8df8e9ed37f0a8f5 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -140,6 +140,15 @@ check-headers:
 sync-headers:
 	@./scripts/sync-headers.sh $(SYNC_HEADERS_ARGS)
 
+coverage-prep:
+	@./scripts/coverage.sh --prep $(if $(FULL_O),--build-dir=$(FULL_O)) $(COVERAGE_ARGS)
+
+coverage:
+	@./scripts/coverage.sh $(if $(FULL_O),--build-dir=$(FULL_O)) $(COVERAGE_ARGS)
+
+coverage-table:
+	@./scripts/coverage.sh --table $(if $(FULL_O),--build-dir=$(FULL_O)) $(COVERAGE_ARGS)
+
 #
 # All other targets get passed through:
 #
@@ -147,4 +156,4 @@ sync-headers:
 	$(print_msg)
 	$(make)
 
-.PHONY: all clean tags TAGS FORCE Makefile build-test build-test-tarball check-headers sync-headers
+.PHONY: all clean tags TAGS FORCE Makefile build-test build-test-tarball check-headers sync-headers coverage coverage-prep coverage-table
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 9f3e248d5c4efff8..6ca39ac86599bcad 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -873,6 +873,10 @@ help:
 	@echo '  check-headers		- check which header copies are out of sync with the kernel'
 	@echo '  sync-headers		- sync those header copies with the kernel, creating commits'
 	@echo '			  (SYNC_HEADERS_ARGS="..." passes options to the script)'
+	@echo '  coverage-prep		- build perf with clang coverage, ready for a root test run'
+	@echo '  coverage		- build perf with clang coverage, run the tests and report'
+	@echo '  coverage-table	- print an ASCII table of the last coverage run'
+	@echo '			  (COVERAGE_ARGS="..." passes options to the script)'
 
 
 DOC_TARGETS := doc man html info pdf
diff --git a/tools/perf/scripts/coverage.sh b/tools/perf/scripts/coverage.sh
new file mode 100755
index 0000000000000000..073d665229697269
--- /dev/null
+++ b/tools/perf/scripts/coverage.sh
@@ -0,0 +1,984 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright (C) 2026, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
+#
+# LLVM source-based coverage for perf: build with clang instrumentation,
+# run the complete perf test suite and report line/function coverage for
+# perf, optionally comparing it against a previously saved report.  -t
+# narrows the tests and -f adds files to the per-file table; without -f
+# the report lists the files with the most uncovered lines, which is
+# where new tests are needed.  --table prints the last run as an ASCII
+# coverage table, like pahole's coverage_table.py.  The build can be
+# split from the test run: --prep builds the instrumented perf and
+# records the build directory, and a later run as root uses that build,
+# skips the build and goes straight to the tests, unless --build is
+# given.  The sampling based tests move the percentages by a percent or
+# two between identical runs.
+
+set -euo pipefail
+
+PERF_DIR=$(cd "$(dirname "$0")/.." && pwd -P)
+TOOLS_DIR=$(cd "$PERF_DIR/.." && pwd -P)
+SRC_DIR=$(cd "$TOOLS_DIR/.." && pwd -P)
+
+# perf links objects from these directories, so a change in any of them
+# makes a build and a profile stale.
+PERF_SOURCES=("$PERF_DIR" "$TOOLS_DIR/lib/perf" "$TOOLS_DIR/lib/api"
+	      "$TOOLS_DIR/lib/subcmd" "$TOOLS_DIR/lib/bpf"
+	      "$TOOLS_DIR/include")
+
+DEFAULT_BUILD_DIR=/tmp/build/perf-coverage
+BUILD_DIR=${BUILD_DIR:-}
+COV_DIR=${COV_DIR:-}
+BUILD_DIR_SET=0
+COV_DIR_SET=0
+[ -n "$BUILD_DIR" ] && BUILD_DIR_SET=1
+[ -n "$COV_DIR" ] && COV_DIR_SET=1
+JOBS=${JOBS:-$(nproc 2>/dev/null || echo 1)}
+LLVM_PROFDATA=${LLVM_PROFDATA:-llvm-profdata}
+LLVM_COV=${LLVM_COV:-llvm-cov}
+
+DEBUG_BUILD=1
+SKIP_BUILD=0
+FORCE_BUILD=0
+REPORT_ONLY=0
+PREP=0
+TABLE=0
+HTML=0
+USE_BASELINE=1
+BASELINE=
+BASELINE_SET=0
+UNCOVERED=${UNCOVERED:-20}
+TOP=${TOP:-20}
+MIN_LINES=${MIN_LINES:-100}
+DWARF_VERSION=${DWARF_VERSION:-4}
+TESTS=()
+
+FOCUS=()
+
+ROOT=0
+[ "$(id -u)" = 0 ] && ROOT=1
+
+say()  { printf '\033[1;34m==>\033[0m %s\n' "$*"; }
+warn() { printf '\033[0;33mwarning:\033[0m %s\n' "$*" >&2; }
+die()  { printf '\033[0;31merror:\033[0m %s\n' "$*" >&2; exit 1; }
+
+usage()
+{
+	cat <<EOF
+Usage: scripts/coverage.sh [options] [test name fragment...]
+
+Builds perf with clang source-based coverage, runs the complete perf
+test suite and reports coverage for perf.  Use -t to run a subset and
+-f to focus the per-file table.
+
+Options:
+  -b, --build-dir DIR    perf build directory (default: $DEFAULT_BUILD_DIR)
+  -c, --cov-dir DIR      profiles and reports (default: BUILD_DIR/cov)
+  -j, --jobs N           parallel build jobs (default: $JOBS)
+  -t, --tests "A;B"      test name fragments, ';' separated (default: all);
+                         a subset run does not update the baseline
+  -f, --focus FILE       add a file to the per-file table (repeatable)
+      --top N            when no -f is given, show the N files with the
+                         most uncovered lines (default: $TOP, 0 to disable)
+      --baseline FILE    compare against FILE, keeping it read-only
+                         (default: COV_DIR/baseline.txt, which a full run
+                         saves for the next one)
+      --no-baseline      do not compare against or update a baseline
+      --uncovered N      uncovered line numbers per focus file (default: $UNCOVERED)
+      --skip-build       reuse the existing build, just run the tests
+      --build            build as root even when a prepared build exists
+      --report-only      reuse build and profiles, just render the report
+      --prep             build the instrumented perf and stop; run the
+                         tests later as root with this script
+  -T, --table            print an ASCII table of the last run and exit,
+                         like pahole's coverage_table.py
+      --optimized        build with perf's default -O3 instead of -O0 -g
+      --dwarf-version N  DWARF version for the instrumented build (default: $DWARF_VERSION)
+      --html             also write an HTML report
+  -h, --help             show this help
+
+Environment:
+  BUILD_DIR, COV_DIR, JOBS, LLVM_COV, LLVM_PROFDATA, UNCOVERED, TOP,
+  MIN_LINES, DWARF_VERSION
+
+As root, without --prep or --build, the script uses the build prepared
+by 'make coverage-prep' when there is one, and builds otherwise.  The
+prepared build is recorded in tools/perf/.coverage-build-dir.
+
+Through make:
+  make -C tools/perf coverage COVERAGE_ARGS="-t 'perf annotate'"
+  make -C tools/perf coverage-prep O=/tmp/build/perf-tools-next
+  make -C tools/perf coverage-table
+
+Examples:
+  scripts/coverage.sh                       # full suite, fresh build
+  scripts/coverage.sh --prep                # build only, test later as root
+  scripts/coverage.sh --report-only         # re-render the last run
+  scripts/coverage.sh --table               # ASCII table of the last run
+  scripts/coverage.sh -t "perf annotate"    # a single test fragment
+  scripts/coverage.sh -f util/evsel.c       # add a file to the table
+EOF
+}
+
+while [ $# -gt 0 ]; do
+	arg=$1
+	optval=
+	case "$arg" in
+	--*=*) optval=${arg#*=}; arg=${arg%%=*}
+	       [ -n "$optval" ] || die "$arg needs a value" ;;
+	esac
+
+	case "$arg" in
+	-b|--build-dir)   BUILD_DIR=${optval:-${2?missing value}}; BUILD_DIR_SET=1
+	                  [ -z "$optval" ] && shift ;;
+	-c|--cov-dir)     COV_DIR=${optval:-${2?missing value}}; COV_DIR_SET=1
+	                  [ -z "$optval" ] && shift ;;
+	-j|--jobs)        JOBS=${optval:-${2?missing value}}; [ -z "$optval" ] && shift ;;
+	-f|--focus)       FOCUS+=("${optval:-${2?missing value}}"); [ -z "$optval" ] && shift ;;
+	--baseline)       BASELINE=${optval:-${2?missing value}}; BASELINE_SET=1
+	                  [ -z "$optval" ] && shift ;;
+	--uncovered)      UNCOVERED=${optval:-${2?missing value}}; [ -z "$optval" ] && shift ;;
+	--top)            TOP=${optval:-${2?missing value}}; [ -z "$optval" ] && shift ;;
+	--dwarf-version)  DWARF_VERSION=${optval:-${2?missing value}}; [ -z "$optval" ] && shift ;;
+	-t|--tests)       IFS=';' read -r -a _t <<< "${optval:-${2?missing value}}"
+	                  TESTS+=("${_t[@]}"); [ -z "$optval" ] && shift ;;
+	--no-baseline)    USE_BASELINE=0 ;;
+	--skip-build)     SKIP_BUILD=1 ;;
+	--build)          FORCE_BUILD=1; SKIP_BUILD=0 ;;
+	--report-only)    REPORT_ONLY=1; SKIP_BUILD=1 ;;
+	--prep)           PREP=1 ;;
+	-T|--table)       TABLE=1; REPORT_ONLY=1; SKIP_BUILD=1 ;;
+	--optimized)      DEBUG_BUILD=0 ;;
+	--html)           HTML=1 ;;
+	-h|--help)        usage; exit 0 ;;
+	-*)               die "unknown option: $arg (see --help)" ;;
+	*)                TESTS+=("$arg") ;;
+	esac
+	shift
+done
+
+#
+# The build directory is recorded by --prep (and by a full run) so a
+# later root run, which does not build, finds it without arguments.
+#
+if [ "$BUILD_DIR_SET" = 0 ] && [ "$PREP" = 0 ] &&
+   { [ "$ROOT" = 1 ] || [ "$SKIP_BUILD" = 1 ] ||
+     [ "$REPORT_ONLY" = 1 ] || [ "$TABLE" = 1 ]; } &&
+   [ -f "$PERF_DIR/.coverage-build-dir" ]; then
+	read -r BUILD_DIR < "$PERF_DIR/.coverage-build-dir"
+fi
+[ -n "$BUILD_DIR" ] || BUILD_DIR=$DEFAULT_BUILD_DIR
+
+if [ "$COV_DIR_SET" = 0 ] && [ -f "$BUILD_DIR/cov/state" ]; then
+	COV_DIR=$(awk -F= '$1 == "COV_DIR" { print $2; exit }' \
+		"$BUILD_DIR/cov/state")
+fi
+[ -n "$COV_DIR" ] || COV_DIR=$BUILD_DIR/cov
+
+# The tests run from the kernel tree root, so keep these absolute.  The
+# Makefile passes FULL_O resolved with readlink -f; resolve here too so an
+# in-tree build directory compares equal to the physical PERF_DIR below.
+abspath()
+{
+	realpath -m "$1"
+}
+BUILD_DIR=$(abspath "$BUILD_DIR")
+COV_DIR=$(abspath "$COV_DIR")
+
+# llvm-cov strips the prefix all reported files share.  A build inside
+# tools/perf makes the rows read perf/<build>/..., so BUILD_REL lets the
+# report leave those generated files out; it is empty for any other layout.
+BUILD_REL=
+case "$BUILD_DIR" in
+"$PERF_DIR"/*) BUILD_REL=${BUILD_DIR#"$PERF_DIR"/} ;;
+esac
+
+[ -n "$BASELINE" ] || BASELINE=$COV_DIR/baseline.txt
+[[ $TOP =~ ^[0-9]+$ ]] || die "--top wants a number, not '$TOP'"
+[[ $MIN_LINES =~ ^[0-9]+$ ]] || die "MIN_LINES wants a number, not '$MIN_LINES'"
+
+# Root does not rebuild a prepared build unless asked: use the perf
+# prepared by coverage-prep, or build when there is none.
+if [ "$ROOT" = 1 ] && [ "$PREP" = 0 ] && [ "$TABLE" = 0 ]; then
+	if [ "$REPORT_ONLY" = 0 ] && [ "$SKIP_BUILD" = 0 ] &&
+	   { [ "$FORCE_BUILD" = 1 ] ||
+	     { [ ! -f "$PERF_DIR/.coverage-build-dir" ] &&
+	       [ ! -x "$BUILD_DIR/perf" ]; }; }; then
+		say "Running as root, building perf in $BUILD_DIR"
+	else
+		SKIP_BUILD=1
+		say "Running as root, using the coverage build in $BUILD_DIR"
+	fi
+fi
+
+#
+# Under sudo the build directory comes from a file the invoking user wrote;
+# root would execute the perf found there and delete and rewrite its cov/
+# directory, so refuse one owned by somebody else or writable by another
+# group or by others.  Every parent is checked too: a directory another
+# user can write lets them swap an entry between this check and the run.
+#
+check_root_path()
+{
+	local path=$1 uid gid mode
+	[ "$ROOT" = 1 ] && [ -n "${SUDO_UID:-}" ] || return 0
+	# abspath() resolves links such as /home -> /var/home on Silverblue
+	# and CoreOS, so the walk below checks their targets.  A path not
+	# resolved that way, like $BUILD_DIR/perf, must not be a link.
+	[ -L "$path" ] && die "$path is a symbolic link, use its target"
+	while :; do
+		if [ -e "$path" ]; then
+			if ! read -r uid gid mode < \
+				<(stat -c '%u %g %a' "$path" 2>/dev/null); then
+				die "could not stat $path"
+			fi
+			# A sticky directory (/tmp) lets anybody create entries
+			# but not replace one that belongs to somebody else.
+			if [ $(( 8#$mode & 8#1000 )) = 0 ]; then
+				[ "$uid" = 0 ] || [ "$uid" = "$SUDO_UID" ] ||
+					die "$path is owned by uid $uid, not by root or $SUDO_UID"
+				# A user private group with umask 002
+				# gives 775, so only the developer's
+				# own group may write here.
+				[ $(( 8#$mode & 8#002 )) = 0 ] ||
+					die "$path is writable by others"
+				if [ $(( 8#$mode & 8#020 )) != 0 ] &&
+				   [ "$gid" != "${SUDO_GID:-}" ]; then
+					die "$path is writable by group $gid"
+				fi
+			fi
+		fi
+		[ "$path" = / ] && return 0
+		path=$(dirname "$path")
+	done
+}
+
+check_root_path "$BUILD_DIR"
+check_root_path "$BUILD_DIR/perf"
+check_root_path "$COV_DIR"
+
+#
+# A root run leaves the files it writes owned by root; give them back to the
+# developer, or the next non-root run cannot clean raw/ or move the baseline.
+# The uid comes from sudo, or from the kernel tree when root runs in a
+# container with the tree mounted from the host.  Only the directories this
+# run created are chowned recursively: a mistyped --cov-dir or --build-dir
+# must not hand a shared or system tree to the developer.
+#
+COV_WRITES=(raw html build.log tests.log report.txt perf.profdata profiles.txt
+	    merge.log baseline.txt baseline.txt.prev)
+
+fix_ownership()
+{
+	local uid gid path
+	[ "$ROOT" = 1 ] || return 0
+	if [ -n "${SUDO_UID:-}" ]; then
+		uid=$SUDO_UID
+		gid=${SUDO_GID:-$SUDO_UID}
+	else
+		read -r uid gid < <(stat -c '%u %g' "$PERF_DIR" 2>/dev/null) || return 0
+	fi
+	[ "$uid" != 0 ] || return 0
+
+	for path in "${COV_WRITES[@]}"; do
+		[ -e "$COV_DIR/$path" ] || continue
+		chown -R "$uid:$gid" "$COV_DIR/$path" ||
+			warn "could not give $COV_DIR/$path back to uid $uid"
+	done
+	if [ "$COV_CREATED" = 1 ]; then
+		chown "$uid:$gid" "$COV_DIR" ||
+			warn "could not give $COV_DIR back to uid $uid"
+	fi
+	if [ "$BUILD_CREATED" = 1 ]; then
+		chown -R "$uid:$gid" "$BUILD_DIR" ||
+			warn "could not give $BUILD_DIR back to uid $uid"
+	fi
+	if [ -n "$BUILD_STAMP" ] && [ -f "$BUILD_STAMP" ]; then
+		# -h: find matches a symlink by its own owner, but chown would
+		# otherwise follow it and change a file outside the build tree.
+		find "$BUILD_DIR" -user 0 -newer "$BUILD_STAMP" \
+			-exec chown -h "$uid:$gid" {} + ||
+			warn "could not give the files root built in $BUILD_DIR back to uid $uid"
+		rm -f "$BUILD_STAMP" || true
+	fi
+	if [ "$STATE_DIR_CREATED" = 1 ] && [ -d "$STATE_DIR" ]; then
+		chown "$uid:$gid" "$STATE_DIR" ||
+			warn "could not give $STATE_DIR back to uid $uid"
+	fi
+	if [ -f "$STATE_FILE" ]; then
+		chown "$uid:$gid" "$STATE_FILE" ||
+			warn "could not give $STATE_FILE back to uid $uid"
+	fi
+	if [ -f "$POINTER" ]; then
+		chown "$uid:$gid" "$POINTER" ||
+			warn "could not give $POINTER back to uid $uid"
+	fi
+}
+
+BUILD_LOG=$COV_DIR/build.log
+TEST_LOG=$COV_DIR/tests.log
+REPORT=$COV_DIR/report.txt
+PROFDATA=$COV_DIR/perf.profdata
+# Discovery point for a later root or --table run, so it stays under
+# BUILD_DIR even when --cov-dir points elsewhere.
+STATE_FILE=$BUILD_DIR/cov/state
+STATE_DIR=$(dirname "$STATE_FILE")
+POINTER=$PERF_DIR/.coverage-build-dir
+
+# What exists before this run writes anything: fix_ownership() chowns a
+# shared COV_DIR or BUILD_DIR only when root created it.
+COV_CREATED=0
+[ -d "$COV_DIR" ] || COV_CREATED=1
+BUILD_CREATED=0
+[ -d "$BUILD_DIR" ] || BUILD_CREATED=1
+STATE_DIR_CREATED=0
+[ -d "$STATE_DIR" ] || STATE_DIR_CREATED=1
+# An existing build directory is not chowned as a whole: a stamp taken
+# when the build starts marks the files a root build writes.
+BUILD_STAMP=
+
+#
+# Sanity checks
+#
+for src in "${PERF_SOURCES[@]}"; do
+	[ -d "$src" ] || die "$src not found"
+done
+case "$BUILD_DIR" in
+"$PERF_DIR"|"$TOOLS_DIR"|"$SRC_DIR")
+	die "$BUILD_DIR would mix generated files with the sources, use -b DIR" ;;
+esac
+
+if [ "$TABLE" = 0 ]; then
+	if [ "$PREP" = 0 ]; then
+		command -v "$LLVM_COV" > /dev/null ||
+			die "$LLVM_COV not found (set LLVM_COV)"
+	fi
+	if [ "$SKIP_BUILD" = 0 ]; then
+		command -v clang > /dev/null ||
+			die "clang not found, it is the only compiler with -fcoverage-mapping"
+	fi
+	if [ "$PREP" = 0 ] && [ "$REPORT_ONLY" = 0 ]; then
+		command -v "$LLVM_PROFDATA" > /dev/null ||
+			die "$LLVM_PROFDATA not found (set LLVM_PROFDATA)"
+	fi
+	mkdir -p "$COV_DIR"
+fi
+
+#
+# Build: clang, DEBUG=1 gives -O0 -g like pahole's coverage build, which
+# keeps the line mapping honest at the cost of slower test runs.  DWARF 4
+# is the default; --dwarf-version 5 describes the workloads linked into
+# perf with DW_OP_addrx, the encoding clang uses for globals in DWARF 5.
+#
+check_coverage_build()
+{
+	[ -x "$BUILD_DIR/perf" ] ||
+		die "no $BUILD_DIR/perf, run 'make -C $PERF_DIR O=$BUILD_DIR" \
+		    "coverage-prep' first, or pass --build"
+	if command -v nm > /dev/null; then
+		# No -q: grep reads all of nm's output, so pipefail does
+		# not see a SIGPIPE from an early exit.
+		nm "$BUILD_DIR/perf" 2>/dev/null |
+			grep '__llvm_profile' > /dev/null ||
+			die "$BUILD_DIR/perf was not built for coverage, run coverage-prep"
+	else
+		warn "nm not found, cannot verify that $BUILD_DIR/perf is instrumented"
+	fi
+}
+
+write_state()
+{
+	mkdir -p "$STATE_DIR" ||
+		die "could not create $STATE_DIR"
+	{
+		printf 'BUILD_DIR=%s\n' "$BUILD_DIR"
+		printf 'COV_DIR=%s\n' "$COV_DIR"
+		printf 'DEBUG_BUILD=%s\n' "$DEBUG_BUILD"
+		printf 'DWARF_VERSION=%s\n' "$DWARF_VERSION"
+	} > "$STATE_FILE" || die "could not write $STATE_FILE"
+
+	if ! printf '%s\n' "$BUILD_DIR" > "$POINTER" 2>/dev/null; then
+		warn "could not write $POINTER, root runs will need -b $BUILD_DIR"
+	fi
+}
+
+#
+# A source file newer than $1, if any, skipping the build directory.
+#
+newer_sources()
+{
+	find "${PERF_SOURCES[@]}" -path "$BUILD_DIR" -prune -o \
+		-name '*.[ch]' -newer "$1" -print -quit 2>/dev/null || true
+}
+
+check_build_freshness()
+{
+	local newer
+	[ -x "$BUILD_DIR/perf" ] || return 0
+	newer=$(newer_sources "$BUILD_DIR/perf")
+	[ -z "$newer" ] ||
+		warn "build is older than source changes" \
+		     "(e.g. ${newer#"$SRC_DIR"/}), rerun coverage-prep"
+}
+
+build_perf()
+{
+	if [ "$SKIP_BUILD" = 1 ]; then
+		check_coverage_build
+		check_build_freshness
+		return
+	fi
+
+	say "Building perf in $BUILD_DIR"
+	mkdir -p "$BUILD_DIR"
+	if [ "$ROOT" = 1 ] && [ "$BUILD_CREATED" = 0 ]; then
+		BUILD_STAMP=$BUILD_DIR/.coverage-stamp
+		: > "$BUILD_STAMP" 2>/dev/null ||
+			{ warn "could not write $BUILD_STAMP"; BUILD_STAMP=; }
+	fi
+	local debug=()
+	[ "$DEBUG_BUILD" = 1 ] && debug=(DEBUG=1)
+
+	if ! make -C "$PERF_DIR" O="$BUILD_DIR" CC=clang \
+		"${debug[@]+"${debug[@]}"}" \
+		EXTRA_CFLAGS="-fprofile-instr-generate -fcoverage-mapping -gdwarf-$DWARF_VERSION" \
+		-j"$JOBS" > "$BUILD_LOG" 2>&1; then
+		tail -20 "$BUILD_LOG" >&2
+		die "build failed, full log in $BUILD_LOG"
+	fi
+	tail -1 "$BUILD_LOG"
+	write_state
+}
+
+#
+# Run the selected tests with the instrumented perf in PATH: the shell
+# tests, the workloads and 'perf test' itself all come from there.
+#
+run_tests()
+{
+	if [ ${#TESTS[@]} -eq 0 ]; then
+		say "Running the complete perf test suite"
+	else
+		say "Running perf tests: ${TESTS[*]}"
+	fi
+	rm -rf "$COV_DIR/raw"
+	mkdir -p "$COV_DIR/raw"
+
+	set +e
+	# The shell tests only get discovered from the kernel tree root.
+	(cd "$SRC_DIR" &&
+		PATH="$BUILD_DIR:$PATH" \
+		LLVM_PROFILE_FILE="$COV_DIR/raw/perf-%p-%m.profraw" \
+			"$BUILD_DIR/perf" test -v \
+				"${TESTS[@]+"${TESTS[@]}"}") 2>&1 | tee "$TEST_LOG"
+	test_status=${PIPESTATUS[0]}
+	set -e
+
+	# 'perf test' exits 0 even when tests fail or time out, only the
+	# summary tells.
+	if [ "$test_status" = 0 ] &&
+	   grep -qE '^(Failed|Timed out) tests +: +[1-9]' "$TEST_LOG"; then
+		test_status=1
+	fi
+
+	grep -E '^(Passed|Skipped|Failed|Timed out) .*:' "$TEST_LOG" || true
+	[ "$test_status" = 0 ] ||
+		warn "some tests failed, coverage is still collected"
+	if [ "$test_status" != 0 ] && grep -q 'Access to performance monitoring' "$TEST_LOG"; then
+		warn "perf_event_paranoid=$(cat /proc/sys/kernel/perf_event_paranoid 2>/dev/null)" \
+		     "denied events to tests recording with callchains;" \
+		     "run as root or lower it for the full suite"
+	fi
+}
+
+#
+# Merge the per-process profiles and render the text report.
+#
+merge_profiles()
+{
+	local count size unusable=0 list=$COV_DIR/profiles.txt
+	# A full run leaves thousands of profiles: llvm-profdata reads the
+	# list from a file so they do not have to fit on the command line.
+	find "$COV_DIR/raw" -name '*.profraw' | sort > "$list"
+	count=$(wc -l < "$list")
+	size=$(du -sh "$COV_DIR/raw" 2>/dev/null | cut -f1)
+	[ "$count" -gt 0 ] ||
+		die "no .profraw files, was perf rebuilt with -fprofile-instr-generate?"
+
+	say "Merging $count profile files ($size)"
+	if ! "$LLVM_PROFDATA" merge -sparse --input-files="$list" \
+			-o "$PROFDATA" 2> "$COV_DIR/merge.log"; then
+		# A file left half written, by a perf killed at exit or by a full
+		# tmpfs, makes llvm-profdata drop the whole merge and with it the
+		# run's measurement: retry keeping what can be read and say how
+		# much was lost.  --failure-mode=warn exists since LLVM 14, the
+		# oldest llvm-profdata the test images ship.
+		warn "some profile files are unusable, merging the rest"
+		"$LLVM_PROFDATA" merge -sparse --failure-mode=warn \
+			--input-files="$list" -o "$PROFDATA" \
+			2> "$COV_DIR/merge.log" ||
+			die "llvm-profdata merge failed, see $COV_DIR/merge.log"
+		# Count only warnings naming a profile: the merge also warns
+		# about counter and hash mismatches.
+		unusable=$(sed -n 's/^warning: \(.*\.profraw\):.*/\1/p' \
+			"$COV_DIR/merge.log" | sort -u | wc -l)
+		[ "$unusable" -lt "$count" ] ||
+			die "no usable profile among $count, see $COV_DIR/merge.log"
+		warn "merged $((count - unusable)) profile files, ignored $unusable unusable"
+	fi
+}
+
+llvm_cov_objects()
+{
+	echo "$BUILD_DIR/perf"
+	local so
+	for so in perf.so libperf.so libperf-jvmti.so; do
+		[ -f "$BUILD_DIR/$so" ] && echo "--object=$BUILD_DIR/$so"
+	done
+	# The Python extension is named after the interpreter ABI and lives
+	# in its own directory.
+	for so in "$BUILD_DIR"/python/perf*.so; do
+		[ -f "$so" ] && echo "--object=$so"
+	done
+}
+
+render_report()
+{
+	local objects
+	mapfile -t objects < <(llvm_cov_objects)
+
+	say "Generating report"
+	"$LLVM_COV" report "${objects[@]}" --instr-profile="$PROFDATA" > "$REPORT"
+}
+
+#
+# The report parsers share these: llvm-cov strips the prefix every
+# reported file has in common, so a tools/perf source can read as
+# perf/util/evsel.c; perf_relpath() gives the path -f takes, generated()
+# spots a build directory row.
+AWK_PERF='
+function perf_relpath(p) {
+	sub(/^.*tools\/perf\//, "", p)
+	sub(/^perf\//, "", p)
+	return p
+}
+function generated(p) {
+	return build != "" && index(perf_relpath(p), build "/") == 1
+}
+'
+
+# Line coverage of a full report for one file: "lines missed cover fn%".
+# Match the requested path as a suffix: a build outside the source tree
+# gives ".../tools/perf/...", one inside it "tools/perf/...", and one
+# inside tools/ makes llvm-cov strip the "<srctree>/tools/" prefix shared
+# by every reported file, giving "perf/...".
+#
+# -f takes the path as it shows in the report, or a tools/perf/ relative one.
+focus_path()
+{
+	case "$1" in
+	*tools/perf/*) printf '%s\n' "${1#*tools/perf/}" ;;
+	*) printf '%s\n' "$1" ;;
+	esac
+}
+
+file_cov()
+{
+	awk -v f="$2" "$AWK_PERF"'
+		{
+			g = perf_relpath(f)
+			rel = perf_relpath($1)
+			if (rel == f || rel == g || $1 == f) {
+				print $8, $9, $10, $7
+				exit
+			}
+		}' "$1"
+}
+
+total_cov()
+{
+	awk '$1 == "TOTAL" { print $8, $9, $10 }' "$1"
+}
+
+total_cov_perf()
+{
+	# Generated files under an in-tree build directory read as
+	# perf/<build>/...; leave them out so an in and an out of tree
+	# build count the same files.
+	awk -v build="$BUILD_REL" "$AWK_PERF"'
+		generated($1) { next }
+		$1 != "TOTAL" &&
+		($1 ~ /(^|\/)tools\/perf\// || $1 ~ /^perf\//) {
+			lines += $8
+			missed += $9
+		}
+		END {
+			if (lines)
+				printf "%d %d %.2f%%\n", lines, missed,
+					100 * (lines - missed) / lines
+		}' "$1"
+}
+
+# Delta between the totals the $3 helper (total_cov or total_cov_perf)
+# prints for $prev and $cur.
+total_delta()
+{
+	local prev=$1 cur=$2 total=$3 old cur_cov
+	[ -f "$prev" ] || return 0
+	old=$("$total" "$prev" | awk '{print $3}')
+	cur_cov=$("$total" "$cur" | awk '{print $3}')
+	[ -n "$old" ] && [ -n "$cur_cov" ] || return 0
+	awk -v cur="$cur_cov" -v old="$old" 'BEGIN {
+		sub(/%$/, "", cur); sub(/%$/, "", old)
+		printf "%+.2f%%", cur - old }'
+}
+
+#
+# "  (previous X, +Y)": the same total in $BASELINE and its delta, when
+# there is a baseline from an earlier run.
+#
+since_baseline()
+{
+	local prev delta
+	[ "$USE_BASELINE" = 1 ] && [ -f "$BASELINE" ] &&
+		! cmp -s "$BASELINE" "$REPORT" || return 0
+	prev=$("$1" "$BASELINE" | awk '{print $3}')
+	delta=$(total_delta "$BASELINE" "$REPORT" "$1")
+	[ -n "$prev" ] && [ -n "$delta" ] || return 0
+	printf "  (previous %s, %s)" "$prev" "$delta"
+}
+
+report_delta()
+{
+	local prev=$1 cur=$2 label
+	local cur_cov old_cov delta
+	label=$(focus_path "$3")
+	cur_cov=$(file_cov "$cur" "$label" | awk '{print $3}')
+	[ -n "$cur_cov" ] || return 0
+
+	if [ -f "$prev" ]; then
+		old_cov=$(file_cov "$prev" "$label" | awk '{print $3}')
+		if [ -n "$old_cov" ]; then
+			delta=$(awk -v cur="$cur_cov" -v old="$old_cov" 'BEGIN {
+				sub(/%$/, "", cur); sub(/%$/, "", old)
+				printf "%+.2f%%", cur - old }')
+		else
+			delta="(new)"
+		fi
+	else
+		old_cov="-"
+		delta="-"
+	fi
+	printf "  %-36s %8s  %8s  %9s\n" "$label" "${old_cov:--}" "$cur_cov" "$delta"
+}
+
+#
+# With no -f, show where the uncovered lines are: the files with the most
+# missed lines are where new tests should try to get more coverage.
+#
+print_worst_files()
+{
+	[ "$TOP" -gt 0 ] || return 0
+	local rows missed lines cover fn path
+	rows=$(awk -v min="$MIN_LINES" -v build="$BUILD_REL" "$AWK_PERF"'
+		($1 ~ /(^|\/)tools\/perf\// || $1 ~ /^perf\//) &&
+		!generated($1) && $8 >= min && $9 > 0 {
+			printf "%s %s %s %s %s\n", $9, $8, $10, $7,
+				perf_relpath($1)
+		}' "$REPORT" | sort -k1,1nr -k2,2nr | sed -n "1,${TOP}p")
+	[ -n "$rows" ] || return 0
+
+	printf "  largest uncovered files (>= %s lines)\n" "$MIN_LINES"
+	printf "  %-44s %8s  %8s  %8s  %8s\n" \
+		"file" "lines" "cover" "missed" "fn-cover"
+	while read -r missed lines cover fn path; do
+		printf "  %-44s %8s  %8s  %8s  %8s\n" \
+			"$path" "$lines" "$cover" "$missed" "$fn"
+	done <<< "$rows"
+}
+
+print_summary()
+{
+	local lines missed cover p_lines p_missed p_cover file cov
+	read -r lines missed cover < <(total_cov "$REPORT")
+	printf "\n\033[1mTotal:\033[0m %s of %s lines covered (%s)" \
+		"$((lines - missed))" "$lines" "$cover"
+	since_baseline total_cov
+	printf "\n"
+	read -r p_lines p_missed p_cover < <(total_cov_perf "$REPORT") || true
+	if [ -n "$p_lines" ]; then
+		printf "  perf: %s of %s lines covered (%s)" \
+			"$((p_lines - p_missed))" "$p_lines" "$p_cover"
+		since_baseline total_cov_perf
+		printf "\n"
+	fi
+	printf "\n"
+
+	if [ ${#FOCUS[@]} -eq 0 ]; then
+		print_worst_files
+		return
+	fi
+
+	printf "  \033[1m%-36s\033[0m %8s  %6s  %6s  %8s\n" \
+		"file" "lines" "cover" "missed" "fn-cover"
+	for file in "${FOCUS[@]}"; do
+		file=$(focus_path "$file")
+		cov=$(file_cov "$REPORT" "$file")
+		if [ -z "$cov" ]; then
+			printf "  %-36s %8s\n" "$file" "not covered"
+			continue
+		fi
+		printf "  %-36s %8s  %6s  %6s  %8s\n" "$file" \
+			"$(echo "$cov" | awk '{print $1}')" \
+			"$(echo "$cov" | awk '{print $3}')" \
+			"$(echo "$cov" | awk '{print $2}')" \
+			"$(echo "$cov" | awk '{print $4}')"
+	done
+
+	if [ "$USE_BASELINE" = 1 ] && [ -f "$BASELINE" ] &&
+	   ! cmp -s "$BASELINE" "$REPORT"; then
+		printf "\n  per file delta since %s\n" "$BASELINE"
+		printf "  %-36s %8s  %8s  %9s\n" "file" "previous" "current" "delta"
+		for file in "${FOCUS[@]}"; do
+			report_delta "$BASELINE" "$REPORT" "$file"
+		done
+	fi
+}
+
+#
+# --table: render the report of the last run as an ASCII table, like
+# pahole's coverage_table.py.  Without -f the rows are the TOP files with
+# the most uncovered lines, sorted by line coverage; --top 0 lists every
+# file with at least MIN_LINES lines and uncovered lines.  The total is
+# the whole report, which also counts the libraries perf links with.
+#
+bar()
+{
+	local pct=$1 width=$2 filled i out=
+	filled=$(awk -v p="$pct" -v w="$width" 'BEGIN { printf "%d", p * w / 100 }')
+	for ((i = 0; i < filled; i++)); do out+=█; done
+	for ((i = filled; i < width; i++)); do out+=░; done
+	printf '%s' "$out"
+}
+
+center()
+{
+	local left=$(( ($2 - ${#1}) / 2 ))
+	printf '%*s%s%*s' "$left" '' "$1" "$(( $2 - ${#1} - left ))" ''
+}
+
+dashes()
+{
+	local s
+	printf -v s '%*s' "$1" ''
+	printf '%s' "${s// /─}"
+}
+
+table_row()
+{
+	awk -v f="$2" "$AWK_PERF"'
+		{
+			g = perf_relpath(f)
+			path = perf_relpath($1)
+			if (path == f || path == g || $1 == f) {
+				printf "%s\t%s\t%d\t%d\t%.2f\t%s\t%d\t%s\n",
+				       path, $8, $8 - $9, $9, $10, $5, $5 - $6, $7
+				exit
+			}
+		}' "$1"
+}
+
+table_rows()
+{
+	if [ ${#FOCUS[@]} -gt 0 ]; then
+		local file
+		for file in "${FOCUS[@]}"; do
+			table_row "$REPORT" "$file"
+		done | sort -t$'\t' -k5,5gr
+		return
+	fi
+
+	awk -v min="$MIN_LINES" -v build="$BUILD_REL" "$AWK_PERF"'
+		($1 ~ /(^|\/)tools\/perf\// || $1 ~ /^perf\//) &&
+		!generated($1) && $8 >= min && $9 > 0 {
+			printf "%s\t%s\t%d\t%d\t%.2f\t%s\t%d\t%s\n",
+			       perf_relpath($1), $8, $8 - $9, $9, $10,
+			       $5, $5 - $6, $7
+		}' "$REPORT" |
+		sort -t$'\t' -k4,4nr |
+		if [ "$TOP" -gt 0 ]; then sed -n "1,${TOP}p"; else cat; fi |
+		sort -t$'\t' -k5,5gr
+}
+
+print_table()
+{
+	local rows total path lines hit missed pct funcs funcs_hit func_cov
+	local row_fmt w d_file d29 d13 d10 d8
+
+	rows=$(table_rows)
+	[ -n "$rows" ] ||
+		die "no coverage data in $REPORT for the requested files"
+	total=$(awk '$1 == "TOTAL" {
+		printf "%s\t%d\t%d\t%.2f\t%s\t%d\t%s\n",
+		       $8, $8 - $9, $9, $10, $5, $5 - $6, $7
+	}' "$REPORT")
+
+	w=5
+	while IFS=$'\t' read -r path _; do
+		if [ ${#path} -gt "$w" ]; then
+			w=${#path}
+		fi
+	done <<< "$rows"
+
+	d8=$(dashes 8)
+	d10=$(dashes 10)
+	d13=$(dashes 13)
+	d29=$(dashes 29)
+	d_file=$(dashes $((w + 2)))
+
+	printf '┌%s┬%s┬%s┬%s┬%s┬%s┬%s┐\n' \
+		"$d_file" "$d8" "$d8" "$d8" "$d29" "$d13" "$d10"
+	printf '│ %-*s │ %6s │ %6s │ %6s │ %s │ %11s │ %8s │\n' \
+		"$w" "File" "Lines" "Hit" "Miss" \
+		"$(center 'Line Coverage' 27)" "Funcs" "FuncCov"
+	printf '├%s┼%s┼%s┼%s┼%s┼%s┼%s┤\n' \
+		"$d_file" "$d8" "$d8" "$d8" "$d29" "$d13" "$d10"
+
+	row_fmt=$(printf '│ %%-%ds │ %%6s │ %%6s │ %%6s │ %%s %%5.1f%%%%' "$w")
+	row_fmt+=' │ %5s/%-5s │ %8s │\n'
+
+	while IFS=$'\t' read -r path lines hit missed pct funcs funcs_hit func_cov; do
+		printf "$row_fmt" "$path" "$lines" "$hit" "$missed" \
+			"$(bar "$pct" 20)" "$pct" "$funcs_hit" "$funcs" "$func_cov"
+	done <<< "$rows"
+
+	printf '├%s┼%s┼%s┼%s┼%s┼%s┼%s┤\n' \
+		"$d_file" "$d8" "$d8" "$d8" "$d29" "$d13" "$d10"
+	while IFS=$'\t' read -r lines hit missed pct funcs funcs_hit func_cov; do
+		printf "$row_fmt" "TOTAL" "$lines" "$hit" "$missed" \
+			"$(bar "$pct" 20)" "$pct" "$funcs_hit" "$funcs" "$func_cov"
+	done <<< "$total"
+	printf '└%s┴%s┴%s┴%s┴%s┴%s┴%s┘\n' \
+		"$d_file" "$d8" "$d8" "$d8" "$d29" "$d13" "$d10"
+}
+
+print_uncovered()
+{
+	[ "$UNCOVERED" -gt 0 ] || return 0
+	[ ${#FOCUS[@]} -gt 0 ] || return 0
+	local objects file out lines header=0
+	mapfile -t objects < <(llvm_cov_objects)
+
+	for file in "${FOCUS[@]}"; do
+		file=$(focus_path "$file")
+		# No report row means the file was not built: print_summary()
+		# already said so, llvm-cov show would dump the whole profile.
+		[ -n "$(file_cov "$REPORT" "$file")" ] || continue
+		out=$("$LLVM_COV" show "${objects[@]}" --instr-profile="$PROFDATA" \
+			"$PERF_DIR/$file" 2>/dev/null |
+			awk -F'|' '$2 ~ /^[[:space:]]*0[[:space:]]*$/ {
+				gsub(/[[:space:]]/, "", $1); printf "%s ", $1 }')
+		lines=$(echo "$out" | wc -w)
+		[ "$lines" -gt 0 ] || continue
+		if [ "$header" = 0 ]; then
+			printf "\n  uncovered lines (first %s per file)\n" "$UNCOVERED"
+			header=1
+		fi
+		printf "  %-24s %4s lines: %s\n" "$file" "$lines" \
+			"$(echo "$out" | cut -d' ' -f1-"$UNCOVERED")"
+	done
+}
+
+save_html()
+{
+	[ "$HTML" = 1 ] || return 0
+	local objects
+	mapfile -t objects < <(llvm_cov_objects)
+	say "Writing HTML report to $COV_DIR/html"
+	"$LLVM_COV" show "${objects[@]}" --instr-profile="$PROFDATA" \
+		--format=html --output-dir="$COV_DIR/html" \
+		--show-line-counts-or-regions --show-branches=count
+}
+
+check_stale()
+{
+	[ "$REPORT_ONLY" = 1 ] || return 0
+	if [ ! -f "$PROFDATA" ]; then
+		[ "$TABLE" = 1 ] && return 0
+		die "no $PROFDATA, run without --report-only first"
+	fi
+	local newer
+	newer=$(newer_sources "$PROFDATA")
+	if [ -n "$newer" ]; then
+		warn "profiles are older than source changes" \
+		     "(e.g. ${newer#"$SRC_DIR"/}), results may be stale"
+	fi
+}
+
+update_baseline()
+{
+	[ "$USE_BASELINE" = 1 ] || return 0
+	[ "$REPORT_ONLY" = 1 ] && return 0
+	# A --baseline file is only read, the default one rolls forward.
+	[ "$BASELINE_SET" = 0 ] || return 0
+	# A -t run covers a subset, saving it would poison the next full run.
+	if [ ${#TESTS[@]} -gt 0 ]; then
+		printf "\nBaseline not updated, this was a subset run\n"
+		return 0
+	fi
+	[ -f "$BASELINE" ] && mv "$BASELINE" "$BASELINE.prev"
+	cp "$REPORT" "$BASELINE"
+	printf "\nBaseline saved: %s\n" "$BASELINE"
+}
+
+test_status=0
+
+if [ "$TABLE" = 1 ]; then
+	[ -f "$REPORT" ] || die "no $REPORT, run coverage first"
+	# The table code parses llvm-cov's decimal points; keep the numeric
+	# locale fixed here only, the tests must run with their normal one.
+	export LC_ALL=C
+	check_stale
+	print_table
+	exit 0
+fi
+
+if [ "$ROOT" = 1 ]; then
+	trap fix_ownership EXIT
+fi
+
+if [ "$PREP" = 1 ]; then
+	build_perf
+	# build_perf() records the build unless it only checked it.
+	if [ "$SKIP_BUILD" = 1 ]; then
+		write_state
+	fi
+	say "Coverage build ready: $BUILD_DIR/perf"
+	printf "As root: %s/scripts/coverage.sh\n" "$PERF_DIR"
+	exit 0
+fi
+
+if [ "$REPORT_ONLY" = 0 ]; then
+	build_perf
+	run_tests
+	merge_profiles
+else
+	check_coverage_build
+	check_stale
+fi
+
+[ -f "$PROFDATA" ] || die "no $PROFDATA, run without --report-only first"
+
+export LC_ALL=C
+render_report
+print_summary
+print_uncovered
+save_html
+update_baseline
+
+printf "\nReport:  %s\n" "$REPORT"
+[ "$HTML" = 1 ] && printf "HTML:    %s\n" "$COV_DIR/html/index.html"
+
+exit "$test_status"
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 03/13] perf annotate: Handle absolute addressing in data type profiling
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
  2026-11-17 20:04 ` [PATCH v6 01/13] perf jvmti: Link the agent with EXTRA_CFLAGS Arnaldo Carvalho de Melo
  2026-11-17 20:04 ` [PATCH v6 02/13] perf scripts: Add a coverage harness for perf Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:35   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 04/13] perf tests: Exercise DW_OP_addrx global variables Arnaldo Carvalho de Melo
                   ` (9 subsequent siblings)
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

32-bit x86 accesses globals with absolute addresses, like
"movl %eax,0x8c73448", but the operand parser only recognized the
offset(reg) forms, so the operand had no memory reference, the resolver
saw a zero data address and resolved nothing.

Parse a bare "0x" number as an absolute memory operand, leaving jumps,
calls, loop* and the 64-bit movabs* out, use the address in the resolver,
and let the instruction tracking record the type of an absolute global
load in the destination register.  The displacement is sign extended for
64-bit objects, as the CPU does, and zero extended for 32-bit ones.
Values that do not fit the int offset, zero, and the operands of
relocatable objects, which are unrelocated addends, are skipped.

With bare numbers being memory operands, a frame base register of -1,
"none", would also compare equal to the -1 "no register" of an absolute
operand and take it for a stack access, so guard those comparisons.
Operands left with no register, like a user space "%gs:imm", are no
longer looked up as variables, which can change the data type stats of
those samples.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/annotate-arch/annotate-x86.c | 52 ++++++++++++++++++--
 tools/perf/util/annotate.c                   | 25 +++++++++-
 tools/perf/util/annotate.h                   |  7 +++
 3 files changed, 79 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/annotate-arch/annotate-x86.c b/tools/perf/util/annotate-arch/annotate-x86.c
index e38a4e7a54d5585c..adb52d148cba6001 100644
--- a/tools/perf/util/annotate-arch/annotate-x86.c
+++ b/tools/perf/util/annotate-arch/annotate-x86.c
@@ -230,6 +230,11 @@ static bool vtable_get_rettype(Dwarf_Die *class_die, int offset,
 	return die_get_real_type(&func_die, type_die) != NULL;
 }
 
+static bool is_stack_or_fb_reg(struct type_state *state, int fbreg, int reg)
+{
+	return (fbreg != -1 && reg == fbreg) || reg == state->stack_reg;
+}
+
 static void update_insn_state_x86(struct type_state *state,
 				  struct data_loc_info *dloc, Dwarf_Die *cu_die,
 				  struct disasm_line *dl)
@@ -437,7 +442,7 @@ static void update_insn_state_x86(struct type_state *state,
 		invalidate_reg_state(tsr);
 
 		/* Case 1: Based on stack pointer or frame pointer */
-		if (sreg == fbreg || sreg == state->stack_reg) {
+		if (is_stack_or_fb_reg(state, fbreg, sreg)) {
 			struct type_state_stack *stack;
 			int offset = src->offset - fboff;
 
@@ -626,7 +631,7 @@ static void update_insn_state_x86(struct type_state *state,
 
 retry:
 		/* Check stack variables with offset */
-		if (sreg == fbreg || sreg == state->stack_reg) {
+		if (is_stack_or_fb_reg(state, fbreg, sreg)) {
 			struct type_state_stack *stack;
 			int offset = src->offset - fboff;
 
@@ -741,6 +746,29 @@ static void update_insn_state_x86(struct type_state *state,
 				     insn_offset, addr, dst->reg1);
 			pr_debug_type_name(&type_die, tsr->kind);
 		}
+		/* Or an absolute address, as used by 32-bit code */
+		else if (annotated_op_loc__is_abs(src, map__dso(dloc->ms->map))) {
+			struct map_symbol *ms = dloc->ms;
+			u64 ip = ms->sym->start + dl->al.offset;
+			u64 addr = annotated_op_loc__abs_addr(src, map__dso(ms->map));
+			int offset;
+
+			if (!get_global_var_type(cu_die, dloc, ip, addr, &offset,
+						 &type_die) ||
+			    !die_get_member_type(&type_die, offset, &type_die)) {
+				invalidate_reg_state(tsr);
+				return;
+			}
+
+			tsr->type = type_die;
+			tsr->kind = TSR_KIND_TYPE;
+			tsr->offset = 0;
+			tsr->ok = true;
+
+			pr_debug_dtp("mov [%x] global addr=%#"PRIx64" -> reg%d",
+				     insn_offset, addr, dst->reg1);
+			pr_debug_type_name(&type_die, tsr->kind);
+		}
 		/* And check percpu access with base register */
 		else if (has_reg_type(state, sreg) &&
 			 state->regs[sreg].kind == TSR_KIND_PERCPU_BASE) {
@@ -827,7 +855,7 @@ static void update_insn_state_x86(struct type_state *state,
 	/* Case 3. register to memory transfers */
 	if (!src->mem_ref && dst->mem_ref) {
 		/* Check stack variables with offset */
-		if (dst->reg1 == fbreg || dst->reg1 == state->stack_reg) {
+		if (is_stack_or_fb_reg(state, fbreg, dst->reg1)) {
 			struct type_state_stack *stack;
 			int offset = dst->offset - fboff;
 
@@ -931,7 +959,7 @@ static int extract_reg_offset(const struct arch *arch, const char *str,
 }
 
 static void extract_op_location_x86(const struct arch *arch,
-				    struct disasm_line *dl __maybe_unused,
+				    struct disasm_line *dl,
 				    const char *op_str, int op_idx __maybe_unused,
 				    struct annotated_op_loc *op_loc)
 {
@@ -960,6 +988,22 @@ static void extract_op_location_x86(const struct arch *arch,
 			op_loc->offset = strtol(s + 1, &p, 0);
 			if (p && p != s + 1)
 				op_loc->imm = true;
+		} else if (!strncmp(s, "0x", 2) && !ins__is_jump(&dl->ins) &&
+			   !ins__is_call(&dl->ins) &&
+			   strncmp(dl->ins.name, "loop", 4) &&
+			   strncmp(dl->ins.name, "movabs", 6)) {
+			u64 offset = strtoull(s, NULL, 0);
+
+			/*
+			 * Keep only what fits the int, as disp32 or as a u32;
+			 * movabs*, the 64-bit form, is left out.
+			 */
+			if (offset != 0 && (offset <= UINT_MAX ||
+					    (s64)(int)offset == (s64)offset)) {
+				/* Absolute address, as used by 32-bit code */
+				op_loc->mem_ref = true;
+				op_loc->offset = offset;
+			}
 		}
 	}
 }
diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 86a4beec6528e9bd..f33e22c20656a4f3 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -2605,6 +2605,21 @@ int arch__dwarf_regnum(const struct arch *arch, const char *str)
 	return reg < 0 ? -1 : reg;
 }
 
+/* A relocatable object's operand is an unrelocated addend, not an address */
+bool annotated_op_loc__is_abs(const struct annotated_op_loc *op_loc,
+			      const struct dso *dso)
+{
+	return !dso__rel(dso) && op_loc->mem_ref && op_loc->reg1 == -1 && op_loc->reg2 == -1 &&
+	       op_loc->segment == INSN_SEG_NONE && op_loc->offset != 0;
+}
+
+/* The disp32 of a 64-bit object is sign extended, a 32-bit one is not */
+u64 annotated_op_loc__abs_addr(const struct annotated_op_loc *op_loc,
+			       const struct dso *dso)
+{
+	return dso__is_64_bit(dso) ? (u64)(s64)op_loc->offset : (u32)op_loc->offset;
+}
+
 /**
  * annotate_get_insn_location - Get location of instruction
  * @arch: the architecture info
@@ -2622,7 +2637,7 @@ int arch__dwarf_regnum(const struct arch *arch, const char *str)
  *   mov  (%rax), %rcx   # src_reg1 = rax, src_mem = 1, src_offset = 0
  *                       # dst_reg1 = rcx, dst_mem = 0
  *
- *   mov  0x18, %r8      # src_reg1 = -1, src_mem = 0
+ *   mov  0x18, %r8      # src_reg1 = -1, src_mem = 1, src_offset = 0x18
  *                       # dst_reg1 = r8, dst_mem = 0
  *
  *   mov  %rsi, 8(%rbx,%rcx,4)  # src_reg1 = rsi, src_mem = 0, src_multi_regs = 0
@@ -2894,6 +2909,11 @@ __hist_entry__get_data_type(struct hist_entry *he, const struct arch *arch,
 			dloc.var_addr = annotate_calc_pcrel(ms, dloc.ip,
 							    op_loc->offset, dl);
 		}
+		/* Absolute addressing, as used by 32-bit code */
+		else if (annotated_op_loc__is_abs(op_loc, map__dso(ms->map))) {
+			dloc.var_addr = annotated_op_loc__abs_addr(op_loc, map__dso(ms->map));
+			op_loc->reg1 = DWARF_REG_PC;
+		}
 
 		/* This CPU access in kernel - pretend PC-relative addressing */
 		if (dso__kernel(map__dso(ms->map)) && arch__is_x86(arch) &&
@@ -2902,6 +2922,9 @@ __hist_entry__get_data_type(struct hist_entry *he, const struct arch *arch,
 			op_loc->reg1 = DWARF_REG_PC;
 		}
 
+		if (op_loc->reg1 == -1)
+			continue;
+
 		mem_type = find_data_type(&dloc);
 
 		if (mem_type == NULL && is_stack_canary(arch, op_loc)) {
diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h
index 1b47c1156b9a72e1..1b0991d4e98c911e 100644
--- a/tools/perf/util/annotate.h
+++ b/tools/perf/util/annotate.h
@@ -18,6 +18,7 @@
 #include "branch.h"
 #include "evsel.h"
 
+struct dso;
 struct hist_browser_timer;
 struct hist_entry;
 struct map;
@@ -638,6 +639,12 @@ struct annotated_insn_loc {
 	     i < INSN_OP_MAX;						\
 	     i++, op_loc++)
 
+/* Bare number operand, as used by 32-bit code to access globals */
+bool annotated_op_loc__is_abs(const struct annotated_op_loc *op_loc,
+			      const struct dso *dso);
+u64 annotated_op_loc__abs_addr(const struct annotated_op_loc *op_loc,
+			       const struct dso *dso);
+
 /* Get detailed location info in the instruction */
 int annotate_get_insn_location(const struct arch *arch, struct disasm_line *dl,
 			       struct annotated_insn_loc *loc);
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 04/13] perf tests: Exercise DW_OP_addrx global variables
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
                   ` (2 preceding siblings ...)
  2026-11-17 20:04 ` [PATCH v6 03/13] perf annotate: Handle absolute addressing in data type profiling Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:13   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 05/13] perf tests: Exercise the flexible array member path Arnaldo Carvalho de Melo
                   ` (8 subsequent siblings)
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

clang emits DW_AT_location with DW_OP_addrx for globals in DWARF 5, gcc
emits DW_OP_addr.  The global_data workload keeps a zero initialized
struct in .bss and the test checks that its accesses are annotated, so
the DW_OP_addrx path is exercised when perf is built with clang and
DWARF 5 (e.g. coverage.sh --dwarf-version 5), and the DW_OP_addr one
with gcc.  It skips on 32-bit PIE builds, which reach static globals
relative to the GOT base register, and the resolver does not track that.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/builtin-test.c            |  1 +
 tools/perf/tests/shell/data_type_global.sh | 68 ++++++++++++++++++++++
 tools/perf/tests/tests.h                   |  1 +
 tools/perf/tests/workloads/Build           |  2 +
 tools/perf/tests/workloads/global_data.c   | 55 +++++++++++++++++
 5 files changed, 127 insertions(+)
 create mode 100755 tools/perf/tests/shell/data_type_global.sh
 create mode 100644 tools/perf/tests/workloads/global_data.c

diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 327483f96401b580..e056a3d3594ff8a2 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -213,6 +213,7 @@ static struct test_workload *workloads[] = {
 	&workload__deterministic,
 	&workload__callchain,
 	&workload__false_sharing,
+	&workload__global_data,
 
 #ifdef HAVE_RUST_SUPPORT
 	&workload__code_with_type,
diff --git a/tools/perf/tests/shell/data_type_global.sh b/tools/perf/tests/shell/data_type_global.sh
new file mode 100755
index 0000000000000000..66c16ffa1729b886
--- /dev/null
+++ b/tools/perf/tests/shell/data_type_global.sh
@@ -0,0 +1,68 @@
+#!/bin/bash
+# perf data type profiling test for a global variable
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
+
+cleanup()
+{
+	rm -f "$perfdata" "$perfout"
+	trap - EXIT TERM INT
+}
+
+trap_cleanup()
+{
+	echo "Unexpected signal in ${FUNCNAME[1]}"
+	cleanup
+	exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+if ! perf check feature -q dwarf; then
+	echo "Skip: no DWARF (libdw) support"
+	cleanup
+	exit 2
+fi
+
+# 32-bit x86 PIE reaches static globals relative to the GOT base register,
+# which the resolver does not track; e_type's low byte is at 16 on LE x86.
+perf_bin=$(command -v perf)
+if [ "$(od -An -tx1 -j4 -N1 "$perf_bin" | tr -d ' ')" = 01 ] &&
+   [ "$(od -An -tx1 -j16 -N1 "$perf_bin" | tr -d ' ')" = 03 ]; then
+	echo "Skip: 32-bit PIE uses GOT-relative global accesses"
+	cleanup
+	exit 2
+fi
+
+# No point in recording if the PMU cannot do memory events.
+if ! perf mem record -o /dev/null -- true 2>/dev/null; then
+	echo "Skip: cannot record memory events on this PMU"
+	cleanup
+	exit 2
+fi
+
+if ! perf mem record -o "$perfdata" -- perf test -w global_data 1 > /dev/null 2>&1; then
+	echo "FAIL: perf mem record"
+	cleanup
+	exit 1
+fi
+
+if ! perf annotate --code-with-type -i "$perfdata" --stdio \
+		--percent-limit 1 > "$perfout"; then
+	echo "FAIL: perf annotate"
+	cleanup
+	exit 1
+fi
+
+if ! grep -q "# data-type: struct conn" "$perfout"; then
+	echo "FAIL: global variable in .bss not resolved"
+	tail -n 30 "$perfout"
+	cleanup
+	exit 1
+fi
+
+echo "global variable resolved"
+cleanup
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index af822885890dda73..049a164503166529 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -273,6 +273,7 @@ DECLARE_WORKLOAD(context_switch_loop);
 DECLARE_WORKLOAD(deterministic);
 DECLARE_WORKLOAD(callchain);
 DECLARE_WORKLOAD(false_sharing);
+DECLARE_WORKLOAD(global_data);
 
 #ifdef HAVE_RUST_SUPPORT
 DECLARE_WORKLOAD(code_with_type);
diff --git a/tools/perf/tests/workloads/Build b/tools/perf/tests/workloads/Build
index 43cc4da6fb0786de..03017aaaaa0bec32 100644
--- a/tools/perf/tests/workloads/Build
+++ b/tools/perf/tests/workloads/Build
@@ -17,6 +17,7 @@ perf-test-y += context_switch_loop.o
 perf-test-y += deterministic.o
 perf-test-y += callchain.o
 perf-test-y += false_sharing.o
+perf-test-y += global_data.o
 
 ifeq ($(CONFIG_RUST_SUPPORT),y)
     perf-test-y += code_with_type.o
@@ -35,3 +36,4 @@ CFLAGS_deterministic.o    = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_named_threads.o    = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_callchain.o        = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_false_sharing.o    = -g -O0 -fno-inline -U_FORTIFY_SOURCE
+CFLAGS_global_data.o      = -g -O0 -fno-inline -U_FORTIFY_SOURCE
diff --git a/tools/perf/tests/workloads/global_data.c b/tools/perf/tests/workloads/global_data.c
new file mode 100644
index 0000000000000000..4e782797363eb41b
--- /dev/null
+++ b/tools/perf/tests/workloads/global_data.c
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A zero initialized global struct, so it lands in .bss, past the file
+ * backed end of the last segment.  clang describes its address with
+ * DW_OP_addrx in DWARF 5, gcc with DW_OP_addr.
+ *
+ * 'perf mem record' of this workload followed by 'perf annotate
+ * --code-with-type' (see tests/shell/data_type_global.sh) resolves the
+ * accesses to struct conn.
+ */
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <linux/compiler.h>
+#include "../tests.h"
+
+struct conn {
+	int saddr;
+	int daddr;
+	long bytes;
+};
+
+static volatile struct conn conn;
+static volatile unsigned long conn_sink;
+static volatile sig_atomic_t done;
+
+static void sighandler(int sig __maybe_unused)
+{
+	done = 1;
+}
+
+static int global_data(int argc, const char **argv)
+{
+	unsigned long sum = 0;
+	unsigned int i = 0;
+	int sec = 1;
+
+	if (argc > 0)
+		sec = atoi(argv[0]);
+
+	signal(SIGINT, sighandler);
+	signal(SIGALRM, sighandler);
+	alarm(sec);
+
+	while (!done) {
+		conn.saddr = i;
+		conn.daddr = i + 1;
+		sum += conn.bytes;
+		i++;
+	}
+	conn_sink = sum;
+	return 0;
+}
+
+DEFINE_WORKLOAD(global_data);
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 05/13] perf tests: Exercise the flexible array member path
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
                   ` (3 preceding siblings ...)
  2026-11-17 20:04 ` [PATCH v6 04/13] perf tests: Exercise DW_OP_addrx global variables Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:11   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 06/13] perf annotate-data: Count flexible array accesses for the whole struct Arnaldo Carvalho de Melo
                   ` (7 subsequent siblings)
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

No test covered find_flex_array() and get_flex_array_member().  Add a
flex_array workload that keeps a struct with a trailing data[] behind a
global pointer, and a test that checks the accesses past the struct size
are resolved and named after the flexible array member; find_data_type()
folds offsets past the struct modulo the element size back to the
member's offset (+0x4 for char data[]).  The test skips on 32-bit PIE
builds, which reach static globals relative to the GOT base register.

Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/builtin-test.c               |  1 +
 .../perf/tests/shell/data_type_flex_array.sh  | 68 +++++++++++++++++++
 tools/perf/tests/tests.h                      |  1 +
 tools/perf/tests/workloads/Build              |  2 +
 tools/perf/tests/workloads/flex_array.c       | 55 +++++++++++++++
 5 files changed, 127 insertions(+)
 create mode 100755 tools/perf/tests/shell/data_type_flex_array.sh
 create mode 100644 tools/perf/tests/workloads/flex_array.c

diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index e056a3d3594ff8a2..8fc425d470c17dbf 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -214,6 +214,7 @@ static struct test_workload *workloads[] = {
 	&workload__callchain,
 	&workload__false_sharing,
 	&workload__global_data,
+	&workload__flex_array,
 
 #ifdef HAVE_RUST_SUPPORT
 	&workload__code_with_type,
diff --git a/tools/perf/tests/shell/data_type_flex_array.sh b/tools/perf/tests/shell/data_type_flex_array.sh
new file mode 100755
index 0000000000000000..850997d94baab07d
--- /dev/null
+++ b/tools/perf/tests/shell/data_type_flex_array.sh
@@ -0,0 +1,68 @@
+#!/bin/bash
+# perf data type profiling tests for flexible array members
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
+
+cleanup()
+{
+	rm -f "$perfdata" "$perfout"
+	trap - EXIT TERM INT
+}
+
+trap_cleanup()
+{
+	echo "Unexpected signal in ${FUNCNAME[1]}"
+	cleanup
+	exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+if ! perf check feature -q dwarf; then
+	echo "Skip: no DWARF (libdw) support"
+	cleanup
+	exit 2
+fi
+
+# 32-bit x86 PIE reaches static globals relative to the GOT base register,
+# which the resolver does not track; e_type's low byte is at 16 on LE x86.
+perf_bin=$(command -v perf)
+if [ "$(od -An -tx1 -j4 -N1 "$perf_bin" | tr -d ' ')" = 01 ] &&
+   [ "$(od -An -tx1 -j16 -N1 "$perf_bin" | tr -d ' ')" = 03 ]; then
+	echo "Skip: 32-bit PIE uses GOT-relative global accesses"
+	cleanup
+	exit 2
+fi
+
+# No point in recording if the PMU cannot do memory events.
+if ! perf mem record -o /dev/null -- true 2>/dev/null; then
+	echo "Skip: cannot record memory events on this PMU"
+	cleanup
+	exit 2
+fi
+
+if ! perf mem record -o "$perfdata" -- perf test -w flex_array 1 > /dev/null 2>&1; then
+	echo "FAIL: perf mem record"
+	cleanup
+	exit 1
+fi
+
+if ! perf annotate --code-with-type -i "$perfdata" --stdio \
+		--percent-limit 1 > "$perfout"; then
+	echo "FAIL: perf annotate"
+	cleanup
+	exit 1
+fi
+
+if ! grep -Fq "# data-type: struct flex +0x4 (data)" "$perfout"; then
+	echo "FAIL: flexible array member not resolved"
+	tail -n 30 "$perfout"
+	cleanup
+	exit 1
+fi
+
+echo "flexible array member resolved"
+cleanup
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 049a164503166529..0cf33d91804f19c4 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -274,6 +274,7 @@ DECLARE_WORKLOAD(deterministic);
 DECLARE_WORKLOAD(callchain);
 DECLARE_WORKLOAD(false_sharing);
 DECLARE_WORKLOAD(global_data);
+DECLARE_WORKLOAD(flex_array);
 
 #ifdef HAVE_RUST_SUPPORT
 DECLARE_WORKLOAD(code_with_type);
diff --git a/tools/perf/tests/workloads/Build b/tools/perf/tests/workloads/Build
index 03017aaaaa0bec32..74a0cbe49dc93533 100644
--- a/tools/perf/tests/workloads/Build
+++ b/tools/perf/tests/workloads/Build
@@ -18,6 +18,7 @@ perf-test-y += deterministic.o
 perf-test-y += callchain.o
 perf-test-y += false_sharing.o
 perf-test-y += global_data.o
+perf-test-y += flex_array.o
 
 ifeq ($(CONFIG_RUST_SUPPORT),y)
     perf-test-y += code_with_type.o
@@ -37,3 +38,4 @@ CFLAGS_named_threads.o    = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_callchain.o        = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_false_sharing.o    = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_global_data.o      = -g -O0 -fno-inline -U_FORTIFY_SOURCE
+CFLAGS_flex_array.o       = -g -O0 -fno-inline -U_FORTIFY_SOURCE
diff --git a/tools/perf/tests/workloads/flex_array.c b/tools/perf/tests/workloads/flex_array.c
new file mode 100644
index 0000000000000000..141f9d510816cb05
--- /dev/null
+++ b/tools/perf/tests/workloads/flex_array.c
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Flexible array member workload for data type profiling: the accesses to
+ * the array are past the end of the struct, where the resolver has to find
+ * the flexible array member.
+ *
+ * See tests/shell/data_type_flex_array.sh.
+ */
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <linux/compiler.h>
+#include "../tests.h"
+
+struct flex {
+	int len;
+	char data[];
+};
+
+static struct flex *gp;
+static volatile unsigned long flex_sink;
+static volatile sig_atomic_t done;
+
+static void sighandler(int sig __maybe_unused)
+{
+	done = 1;
+}
+
+static int flex_array(int argc, const char **argv)
+{
+	unsigned long sum = 0;
+	int sec = 1;
+
+	if (argc > 0)
+		sec = atoi(argv[0]);
+
+	gp = calloc(1, sizeof(*gp) + 4096);
+	if (gp == NULL)
+		return 1;
+	gp->len = 4096;
+
+	signal(SIGINT, sighandler);
+	signal(SIGALRM, sighandler);
+	alarm(sec);
+
+	while (!done) {
+		sum += gp->data[3];
+		gp->data[5] = 1;
+	}
+	flex_sink = sum;
+	free(gp);
+	return 0;
+}
+
+DEFINE_WORKLOAD(flex_array);
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 06/13] perf annotate-data: Count flexible array accesses for the whole struct
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
                   ` (4 preceding siblings ...)
  2026-11-17 20:04 ` [PATCH v6 05/13] perf tests: Exercise the flexible array member path Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:10   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 07/13] perf tests: Exercise data type resolution from a register Arnaldo Carvalho de Melo
                   ` (6 subsequent siblings)
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

The per-member percentage sums the histogram entries in the member's
own [offset, offset + size) range.  A flexible array member is resolved
past the end of its struct, so the struct row only covered its declared
size and showed 0.00% while the member row showed 100.00% and the
header said N samples.

Take a member's extent as the end of its last child as well, so the
struct counts what was resolved past it.  Union members all start at
offset 0, so a plain union keeps its size; a member that is itself a
struct with a flexible array extends the union too.  The TUI browser
accounts the members the same way, use the extent there as well.

Extend tests/shell/data_type_flex_array.sh to check that the accesses
count for the struct in the percent, samples and period columns.

Fixes: aaca646915a1bb53 ("perf annotate-data: Adjust type offset for flex-array")
Reviewed-by: Ian Rogers <irogers@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 .../perf/tests/shell/data_type_flex_array.sh  | 42 ++++++++++++++++++-
 tools/perf/ui/browsers/annotate-data.c        |  7 ++--
 tools/perf/util/annotate-data.c               | 25 ++++++++++-
 tools/perf/util/annotate-data.h               |  8 ++++
 4 files changed, 75 insertions(+), 7 deletions(-)

diff --git a/tools/perf/tests/shell/data_type_flex_array.sh b/tools/perf/tests/shell/data_type_flex_array.sh
index 850997d94baab07d..224380577fc45fc8 100755
--- a/tools/perf/tests/shell/data_type_flex_array.sh
+++ b/tools/perf/tests/shell/data_type_flex_array.sh
@@ -64,5 +64,45 @@ if ! grep -Fq "# data-type: struct flex +0x4 (data)" "$perfout"; then
 	exit 1
 fi
 
-echo "flexible array member resolved"
+# The accesses to the flexible array are resolved past the end of the
+# struct: they must count for the struct, not only for the member.
+if ! perf annotate --data-type -i "$perfdata" --stdio > "$perfout"; then
+	echo "FAIL: perf annotate --data-type"
+	cleanup
+	exit 1
+fi
+
+if ! awk '
+	$1 + 0 == 100.00 && /struct flex/ { struct_ok = 1 }
+	$1 + 0 > 0 && /char\[\]/ && /data;/ { data_ok = 1 }
+	END { exit !(struct_ok && data_ok) }
+' <(sed -n "/^Annotate type: 'struct flex' in/,/^Annotate type: /p" "$perfout"); then
+	echo "FAIL: --data-type does not count the flexible array accesses"
+	tail -n 30 "$perfout"
+	cleanup
+	exit 1
+fi
+
+# Same with the samples and period columns, the other two display modes.
+for mode in --show-nr-samples --show-total-period; do
+	if ! perf annotate --data-type -i "$perfdata" --stdio "$mode" > "$perfout"; then
+		echo "FAIL: perf annotate --data-type $mode"
+		cleanup
+		exit 1
+	fi
+
+	if ! awk '
+		$1 + 0 > 0 && /struct flex/ { total = $1 + 0 }
+		/int/ && /len;/ { len = $1 + 0 }
+		/char\[\]/ && /data;/ { data = $1 + 0 }
+		END { exit !(total > 0 && data > 0 && total == data + len) }
+	' <(sed -n "/^Annotate type: 'struct flex' in/,/^Annotate type: /p" "$perfout"); then
+		echo "FAIL: --data-type $mode does not count the flexible array accesses"
+		tail -n 30 "$perfout"
+		cleanup
+		exit 1
+	fi
+done
+
+echo "flexible array member resolved and counted"
 cleanup
diff --git a/tools/perf/ui/browsers/annotate-data.c b/tools/perf/ui/browsers/annotate-data.c
index 0f778788df4a934b..e4047e97e8a943a4 100644
--- a/tools/perf/ui/browsers/annotate-data.c
+++ b/tools/perf/ui/browsers/annotate-data.c
@@ -53,12 +53,11 @@ static int get_member_overhead(struct annotated_data_type *adt,
 			       struct evsel *leader)
 {
 	struct annotated_member *member = entry->data;
-	int i, k;
+	int i, k, end = annotated_member__extent(member);
 
-	for (i = 0; i < member->size; i++) {
+	for (i = member->offset; i < end; i++) {
 		struct type_hist *h;
 		struct evsel *evsel;
-		int offset = member->offset + i;
 
 		k = 0;
 		for_each_group_evsel(evsel, leader) {
@@ -69,7 +68,7 @@ static int get_member_overhead(struct annotated_data_type *adt,
 				continue;
 
 			h = &adt->histograms[evsel->core.idx];
-			if (hashmap__find(&h->samples, offset, &hist))
+			if (hashmap__find(&h->samples, i, &hist))
 				update_hist_entry(&entry->hists[k], hist);
 			k++;
 		}
diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c
index 2332d465f9751ef3..19c1de5c29853f69 100644
--- a/tools/perf/util/annotate-data.c
+++ b/tools/perf/util/annotate-data.c
@@ -2208,6 +2208,26 @@ static void print_annotated_data_value(struct type_hist *h, u64 period, int nr_s
 		color_fprintf(stdout, color, " %7.2f", percent);
 }
 
+/*
+ * A flexible array member is resolved past the end of its parent, so the
+ * parent's samples are not bounded by its size.  A GNU extension non-last
+ * member with a flexible array can overlap the next member and double-count.
+ */
+int annotated_member__extent(struct annotated_member *member)
+{
+	struct annotated_member *child;
+	int end = member->offset + member->size;
+
+	list_for_each_entry(child, &member->children, node) {
+		int child_end = annotated_member__extent(child);
+
+		if (child_end > end)
+			end = child_end;
+	}
+
+	return end;
+}
+
 static void print_annotated_data_type(struct annotated_data_type *mem_type,
 				      struct annotated_member *member,
 				      struct evsel *evsel, int indent)
@@ -2215,6 +2235,7 @@ static void print_annotated_data_type(struct annotated_data_type *mem_type,
 	struct annotated_member *child;
 	struct type_hist *h;
 	int i, nr_events = 0, samples = 0;
+	int end = annotated_member__extent(member);
 	u64 period = 0;
 	int width = symbol_conf.show_total_period ? 11 : 7;
 	struct evsel *pos;
@@ -2228,10 +2249,10 @@ static void print_annotated_data_type(struct annotated_data_type *mem_type,
 
 		samples = 0;
 		period = 0;
-		for (i = 0; i < member->size; i++) {
+		for (i = member->offset; i < end; i++) {
 			struct type_hist_entry *entry;
 
-			if (!hashmap__find(&h->samples, member->offset + i, &entry))
+			if (!hashmap__find(&h->samples, i, &entry))
 				continue;
 
 			samples += entry->nr_samples;
diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h
index 184262c7356213fa..6e0d410929651426 100644
--- a/tools/perf/util/annotate-data.h
+++ b/tools/perf/util/annotate-data.h
@@ -260,6 +260,9 @@ void global_var_type__tree_delete(struct rb_root *root);
 /* Print data type annotation (including members) on stdout */
 int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel);
 
+/* End offset of a member, including a flexible array's children */
+int annotated_member__extent(struct annotated_member *member);
+
 /* Get name of member field at the given offset in the data type */
 int annotated_data_type__get_member_name(struct annotated_data_type *adt,
 					 char *buf, size_t sz, int member_offset);
@@ -316,6 +319,11 @@ static inline int hist_entry__annotate_data_tty(struct hist_entry *he __maybe_un
 	return -1;
 }
 
+static inline int annotated_member__extent(struct annotated_member *member)
+{
+	return member->offset + member->size;
+}
+
 static inline int annotated_data_type__get_member_name(struct annotated_data_type *adt __maybe_unused,
 						       char *buf __maybe_unused,
 						       size_t sz __maybe_unused,
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 07/13] perf tests: Exercise data type resolution from a register
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
                   ` (5 preceding siblings ...)
  2026-11-17 20:04 ` [PATCH v6 06/13] perf annotate-data: Count flexible array accesses for the whole struct Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:20   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 08/13] perf annotate: Print the stats after the annotations Arnaldo Carvalho de Melo
                   ` (5 subsequent siblings)
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

At -O2 a struct pointer lives in a register (with DWARF describing it with
a register location on register-argument ABIs), while the unoptimized
workloads the data type tests profile keep their variables on the stack.

Add a regptr workload with a noinline function that takes the pointer from
a volatile global, so the compiler cannot constant-propagate the object
address, and check that its three member accesses are resolved from that
register.

Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/builtin-test.c              |  1 +
 tools/perf/tests/shell/data_type_register.sh | 60 +++++++++++++++++++
 tools/perf/tests/tests.h                     |  1 +
 tools/perf/tests/workloads/Build             |  2 +
 tools/perf/tests/workloads/regptr.c          | 61 ++++++++++++++++++++
 5 files changed, 125 insertions(+)
 create mode 100755 tools/perf/tests/shell/data_type_register.sh
 create mode 100644 tools/perf/tests/workloads/regptr.c

diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 8fc425d470c17dbf..fe4d4cd54f85d502 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -215,6 +215,7 @@ static struct test_workload *workloads[] = {
 	&workload__false_sharing,
 	&workload__global_data,
 	&workload__flex_array,
+	&workload__regptr,
 
 #ifdef HAVE_RUST_SUPPORT
 	&workload__code_with_type,
diff --git a/tools/perf/tests/shell/data_type_register.sh b/tools/perf/tests/shell/data_type_register.sh
new file mode 100755
index 0000000000000000..0d4a420cad9475cd
--- /dev/null
+++ b/tools/perf/tests/shell/data_type_register.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+# perf data type profiling tests for register variables
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
+
+cleanup()
+{
+	rm -f "$perfdata" "$perfout"
+	trap - EXIT TERM INT
+}
+
+trap_cleanup()
+{
+	echo "Unexpected signal in ${FUNCNAME[1]}"
+	cleanup
+	exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+if ! perf check feature -q dwarf; then
+	echo "Skip: no DWARF (libdw) support"
+	cleanup
+	exit 2
+fi
+
+# No point in recording if the PMU cannot do memory events.
+if ! perf mem record -o /dev/null -- true 2>/dev/null; then
+	echo "Skip: cannot record memory events on this PMU"
+	cleanup
+	exit 2
+fi
+
+if ! perf mem record -o "$perfdata" -- perf test -w regptr 1 > /dev/null 2>&1; then
+	echo "FAIL: perf mem record"
+	cleanup
+	exit 1
+fi
+
+if ! perf annotate --code-with-type -i "$perfdata" --stdio \
+		--percent-limit 1 > "$perfout"; then
+	echo "FAIL: perf annotate"
+	cleanup
+	exit 1
+fi
+
+for member in "+0 (a)" "+0x8 (b)" "+0x10 (c)"; do
+	if ! grep -Fq "# data-type: struct node ${member}" "$perfout"; then
+		echo "FAIL: member ${member} not resolved from the register"
+		tail -n 30 "$perfout"
+		cleanup
+		exit 1
+	fi
+done
+
+echo "register held access resolved"
+cleanup
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 0cf33d91804f19c4..b03c56d5e4c98aaf 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -275,6 +275,7 @@ DECLARE_WORKLOAD(callchain);
 DECLARE_WORKLOAD(false_sharing);
 DECLARE_WORKLOAD(global_data);
 DECLARE_WORKLOAD(flex_array);
+DECLARE_WORKLOAD(regptr);
 
 #ifdef HAVE_RUST_SUPPORT
 DECLARE_WORKLOAD(code_with_type);
diff --git a/tools/perf/tests/workloads/Build b/tools/perf/tests/workloads/Build
index 74a0cbe49dc93533..17189458032c0a17 100644
--- a/tools/perf/tests/workloads/Build
+++ b/tools/perf/tests/workloads/Build
@@ -19,6 +19,7 @@ perf-test-y += callchain.o
 perf-test-y += false_sharing.o
 perf-test-y += global_data.o
 perf-test-y += flex_array.o
+perf-test-y += regptr.o
 
 ifeq ($(CONFIG_RUST_SUPPORT),y)
     perf-test-y += code_with_type.o
@@ -39,3 +40,4 @@ CFLAGS_callchain.o        = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_false_sharing.o    = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_global_data.o      = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_flex_array.o       = -g -O0 -fno-inline -U_FORTIFY_SOURCE
+CFLAGS_regptr.o           = -g -O2
diff --git a/tools/perf/tests/workloads/regptr.c b/tools/perf/tests/workloads/regptr.c
new file mode 100644
index 0000000000000000..441202b61ae7b2e6
--- /dev/null
+++ b/tools/perf/tests/workloads/regptr.c
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A struct pointer that -O2 keeps in a register, so the member accesses
+ * have to be resolved from the register's type state and not from a
+ * stack slot.
+ *
+ * See tests/shell/data_type_register.sh.
+ */
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <linux/compiler.h>
+#include "../tests.h"
+
+struct node {
+	long long a;
+	long long b;
+	long long c;
+};
+
+static struct node n;
+static struct node *volatile vp;
+static volatile unsigned long long reg_sink;
+static volatile sig_atomic_t done;
+
+static void sighandler(int sig __maybe_unused)
+{
+	done = 1;
+}
+
+static noinline long long touch(struct node *p)
+{
+	long long sum = 0;
+
+	sum += p->a;
+	/* A load too, or a load-only PMU never resolves b. */
+	p->b += sum;
+	sum += p->c;
+	return sum;
+}
+
+static int regptr(int argc, const char **argv)
+{
+	unsigned long long sum = 0;
+	int sec = 1;
+
+	if (argc > 0)
+		sec = atoi(argv[0]);
+
+	vp = &n;
+	signal(SIGINT, sighandler);
+	signal(SIGALRM, sighandler);
+	alarm(sec);
+
+	while (!done)
+		sum += touch(vp);
+	reg_sink = sum;
+	return 0;
+}
+
+DEFINE_WORKLOAD(regptr);
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 08/13] perf annotate: Print the stats after the annotations
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
                   ` (6 preceding siblings ...)
  2026-11-17 20:04 ` [PATCH v6 07/13] perf tests: Exercise data type resolution from a register Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:11   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 09/13] perf tests: Silence the libbfd probe in build-test Arnaldo Carvalho de Melo
                   ` (4 subsequent siblings)
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

The instruction stats are printed from hists__find_annotations() before
that event is annotated: the first event reports zeros and later events
can report stale counts. The data type total is only counted in
--data-type mode, so --type-stat without it is incomplete too.

Print the stats once after all events have been annotated, warning if
--type-stat is used without --data-type. Leave the TUI's alternate
screen before printing, so the stats remain on the terminal.

Fixes: 58824fa0087e1cb7 ("perf annotate: Add --insn-stat option for debugging")
Fixes: 61a9741e9f78c64c ("perf annotate: Add --type-stat option for debugging")
Cc: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-annotate.c            | 19 +++++--
 tools/perf/tests/shell/annotate_stats.sh | 69 ++++++++++++++++++++++++
 2 files changed, 83 insertions(+), 5 deletions(-)
 create mode 100755 tools/perf/tests/shell/annotate_stats.sh

diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 331adbdbd635917d..8ced8a4aebf07709 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -414,11 +414,6 @@ static void hists__find_annotations(struct hists *hists,
 	struct rb_node *nd = rb_first_cached(&hists->entries), *next;
 	int key = K_RIGHT;
 
-	if (ann->type_stat)
-		print_annotate_data_stat(&ann_data_stat);
-	if (ann->insn_stat)
-		print_annotate_item_stat(&ann_insn_stat, "Instruction");
-
 	while (nd) {
 		struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
 		struct annotation *notes;
@@ -660,6 +655,17 @@ static int __cmd_annotate(struct perf_annotate *ann)
 		hists__find_annotations(hists, pos, ann);
 	}
 
+	if (use_browser == 1 && (ann->insn_stat || (ann->data_type && ann->type_stat))) {
+		/* Leave the alternate screen before printing to the terminal. */
+		ui__exit(false);
+		use_browser = 0;
+	}
+
+	if (ann->data_type && ann->type_stat)
+		print_annotate_data_stat(&ann_data_stat);
+	if (ann->insn_stat)
+		print_annotate_item_stat(&ann_insn_stat, "Instruction");
+
 	if (use_browser == 2) {
 		void (*show_annotations)(void);
 
@@ -865,6 +871,9 @@ int cmd_annotate(int argc, const char **argv)
 	if (quiet)
 		perf_quiet_option();
 
+	if (annotate.type_stat && !annotate.data_type)
+		pr_warning("Warning: --type-stat needs --data-type\n");
+
 	data.path = input_name;
 
 	perf_tool__init(&annotate.tool, /*ordered_events=*/true);
diff --git a/tools/perf/tests/shell/annotate_stats.sh b/tools/perf/tests/shell/annotate_stats.sh
new file mode 100755
index 0000000000000000..5af48bf187c5e8f5
--- /dev/null
+++ b/tools/perf/tests/shell/annotate_stats.sh
@@ -0,0 +1,69 @@
+#!/bin/bash
+# perf annotate instruction stats test
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
+perferr=$(mktemp /tmp/__perf_test.perf.err.XXXXX)
+
+cleanup()
+{
+	rm -f "$perfdata" "$perfout" "$perferr"
+	trap - EXIT TERM INT
+}
+
+trap_cleanup()
+{
+	echo "Unexpected signal in ${FUNCNAME[1]}"
+	cleanup
+	exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+if ! perf check feature -q dwarf; then
+	echo "Skip: no DWARF (libdw) support"
+	cleanup
+	exit 2
+fi
+
+# No point in recording if the PMU cannot do memory events.
+if ! perf mem record -o /dev/null -- true 2>/dev/null; then
+	echo "Skip: cannot record memory events on this PMU"
+	cleanup
+	exit 2
+fi
+
+if ! perf mem record -o "$perfdata" -- perf test -w datasym > /dev/null 2>&1; then
+	echo "FAIL: perf mem record"
+	cleanup
+	exit 1
+fi
+
+if ! perf annotate --code-with-type --insn-stat -i "$perfdata" --stdio \
+		--percent-limit 1 > "$perfout"; then
+	echo "FAIL: perf annotate"
+	cleanup
+	exit 1
+fi
+
+# The instruction stats are accounted while the instructions are
+# displayed; printing them before the display loop shows zeros.
+if ! grep -Eq '^total [1-9][0-9]*,' "$perfout"; then
+	echo "FAIL: instruction stats not accounted"
+	tail -n 30 "$perfout"
+	cleanup
+	exit 1
+fi
+
+if ! perf annotate --type-stat -i "$perfdata" --stdio > /dev/null 2> "$perferr" ||
+   ! grep -Fxq "Warning: --type-stat needs --data-type" "$perferr"; then
+	echo "FAIL: missing --type-stat warning"
+	tail -n 30 "$perferr"
+	cleanup
+	exit 1
+fi
+
+echo "instruction stats accounted"
+cleanup
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 09/13] perf tests: Silence the libbfd probe in build-test
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
                   ` (7 preceding siblings ...)
  2026-11-17 20:04 ` [PATCH v6 08/13] perf annotate: Print the stats after the annotations Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:08   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 10/13] perf build: Record the display-only features in FEATURE-DUMP Arnaldo Carvalho de Melo
                   ` (3 subsequent siblings)
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

build-test evaluates the libbfd probe at parse time and the compiler's
stderr isn't captured by $(shell ...), so without binutils-devel it
prints "bfd.h: No such file or directory" once per tests/make
invocation, five times per run.

Redirect the compiler's stderr; the empty result still skips the
nondistro tests as intended.

Fixes: 523471c5163659c6 ("perf build: Skip nondistro build test if libbfd is old")
Reviewed-by: Ian Rogers <irogers@google.com>
Cc: James Clark <james.clark@linaro.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/make | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/tests/make b/tools/perf/tests/make
index 7524de5228029e7e..e6d7ac49b5b1693d 100644
--- a/tools/perf/tests/make
+++ b/tools/perf/tests/make
@@ -128,7 +128,7 @@ make_minimal        += NO_SDT=1 NO_JVMTI=1 NO_LIBZSTD=1
 make_minimal        += NO_CAPSTONE=1
 
 # binutils 2_42 and newer have bfd_thread_init()
-new_libbfd := $(shell echo '#include <bfd.h>' | $(CC) -E -x c - | grep bfd_thread_init)
+new_libbfd := $(shell echo '#include <bfd.h>' | $(CC) -E -x c - 2>/dev/null | grep bfd_thread_init)
 
 # Whether libLLVM can be built, which needs the llvm-devel/llvm-dev headers and
 # libraries of version 13 or newer, not just llvm-config. This mirrors the
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 10/13] perf build: Record the display-only features in FEATURE-DUMP
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
                   ` (8 preceding siblings ...)
  2026-11-17 20:04 ` [PATCH v6 09/13] perf tests: Silence the libbfd probe in build-test Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:15   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 11/13] perf tests: Fix the Intel PT virtual LBR check Arnaldo Carvalho de Melo
                   ` (2 subsequent siblings)
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

llvm-perf and rust are in FEATURE_DISPLAY but not in FEATURE_TESTS: they
can't be part of the test-all fast path, which sets every FEATURE_TESTS
entry to 1 when test-all.c builds.  Since FEATURE-DUMP is written from
FEATURE_TESTS only, a FEATURES_DUMP= build, as used by build-test, never
sees them and silently builds without libLLVM and the rust workloads.

Append the display features that aren't in FEATURE_TESTS to the dump
after the regular feature checks have run.

Fixes: 206dcfca1f5cfac3 ("perf build: Autodetect minimum required llvm-dev version")
Fixes: 3f5dfa472ea6771c ("tools build: Fix rust feature detection")
Cc: James Clark <james.clark@linaro.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/Makefile.config | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index 78423b0dcc73ac14..baef6b534e8aa8dc 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -1313,3 +1313,7 @@ endif
 # tests.
 $(shell rm -f $(FEATURE_DUMP_FILENAME))
 $(foreach feat,$(FEATURE_TESTS),$(shell echo "$(call feature_assign,$(feat))" >> $(FEATURE_DUMP_FILENAME)))
+# Features only in FEATURE_DISPLAY aren't part of the test-all fast path, so
+# they have to be checked explicitly and recorded here too, otherwise a
+# FEATURES_DUMP= build silently disables them (llvm-perf, rust).
+$(foreach feat,$(filter-out $(FEATURE_TESTS),$(FEATURE_DISPLAY)),$(shell echo "$(call feature_assign,$(feat))" >> $(FEATURE_DUMP_FILENAME)))
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 11/13] perf tests: Fix the Intel PT virtual LBR check
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
                   ` (9 preceding siblings ...)
  2026-11-17 20:04 ` [PATCH v6 10/13] perf build: Record the display-only features in FEATURE-DUMP Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:18   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 12/13] perf test: Drop samples without an IP in the dlfilter test Arnaldo Carvalho de Melo
  2026-11-17 20:04 ` [PATCH v6 13/13] perf test: Skip the LBR inline unwinding check when unsupported Arnaldo Carvalho de Melo
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

With one data page the virtual LBR record loses its AUX samples:

  Warning: Processed 29 samples and lost 100.00%

Use --per-thread with 4 data and 64 AUX pages. Its single 276 KiB
buffer (including metadata on 4 KiB pages) fits the default 516 KiB
mlock allowance, regardless of CPU count. Also require the Python perf
module to export session: importing an unrelated perf module used
to fail the check instead of skipping it.

Fixes: 915d6b14dc0c ("perf test: Migrate Intel PT virtual LBR test to Python API")
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/shell/test_intel_pt.sh | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/tools/perf/tests/shell/test_intel_pt.sh b/tools/perf/tests/shell/test_intel_pt.sh
index c0ecece0a8255124..ba4ed6dc6dd87a5b 100755
--- a/tools/perf/tests/shell/test_intel_pt.sh
+++ b/tools/perf/tests/shell/test_intel_pt.sh
@@ -384,13 +384,14 @@ test_virtual_lbr()
 	# shellcheck source=lib/setup_python.sh
 	. "${shelldir}"/lib/setup_python.sh
 
-	if ! "$PYTHON" -c 'import perf' > /dev/null 2>&1; then
-		echo "SKIP: Python perf module not found"
+	if ! "$PYTHON" -c 'import perf; perf.session' > /dev/null 2>&1; then
+		echo "SKIP: Python perf module with session support not found"
 		return 2
 	fi
 
-	# Check if virtual lbr is working
-	perf_record_no_bpf -o "${tmpfile}" --aux-sample \
+	# A single data page overruns aux samples; use one per-thread buffer
+	# with 4 data and 64 aux pages, plus a metadata page (276 KiB).
+	perf_record_no_bpf --per-thread -m 4,64 -o "${tmpfile}" --aux-sample \
 		-e '{intel_pt//,cycles}:u' perf test -w brstack || return 1
 	perf inject --itrace=L -i "${tmpfile}" -o "${perfdatafile}" || return 1
 	output=$("$PYTHON" "${shelldir}/lib/perf_brstack_max.py" -i "${perfdatafile}") || return 1
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 12/13] perf test: Drop samples without an IP in the dlfilter test
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
                   ` (10 preceding siblings ...)
  2026-11-17 20:04 ` [PATCH v6 11/13] perf tests: Fix the Intel PT virtual LBR check Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:12   ` sashiko-bot
  2026-11-17 20:04 ` [PATCH v6 13/13] perf test: Skip the LBR inline unwinding check when unsupported Arnaldo Carvalho de Melo
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

Since v7.3 the kernel zeroes PERF_SAMPLE_IP when a userspace-only event
skids into kernel mode, so that kernel register state is not exposed to
an unprivileged event (166f10836a653dfa "perf/core: Fix kernel register
info leak via hardware skid").

The test dlfilter keeps those samples, and the check that nothing but
test_loop survives then trips on their lines: the comm column reads
"perf".  Drop them, which is what the test wants, everything except
test_loop must be filtered out.

Fixes: 199d5e872a9ce737 ("perf tests script dlfilter: Add a dlfilter test")
Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/shell/script_dlfilter.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/tests/shell/script_dlfilter.sh b/tools/perf/tests/shell/script_dlfilter.sh
index 88222280f6094fd0..797cd9ae84141f64 100755
--- a/tools/perf/tests/shell/script_dlfilter.sh
+++ b/tools/perf/tests/shell/script_dlfilter.sh
@@ -46,7 +46,7 @@ int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ct
 	const struct perf_dlfilter_al *al;
 
 	if (!sample->ip)
-		return 0;
+		return 1;
 
 	al = perf_dlfilter_fns.resolve_ip(ctx);
 	if (!al || !al->sym || strcmp(al->sym, "test_loop"))
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v6 13/13] perf test: Skip the LBR inline unwinding check when unsupported
  2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
                   ` (11 preceding siblings ...)
  2026-11-17 20:04 ` [PATCH v6 12/13] perf test: Drop samples without an IP in the dlfilter test Arnaldo Carvalho de Melo
@ 2026-11-17 20:04 ` Arnaldo Carvalho de Melo
  2026-11-17 20:29   ` sashiko-bot
  12 siblings, 1 reply; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-11-17 20:04 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Clark Williams, linux-kernel, linux-perf-users,
	Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <acme@redhat.com>

The LBR check considers caps/branches sufficient to record cycles
with --call-graph lbr. AMD BRS advertises branches but samples only
its own retired branches event, so the test fails instead of
skipping.

Skip only when evsel__open_strerror() reports "doesn't support
branch stack sampling" (EOPNOTSUPP). Generic EINVAL or "not
supported" may indicate a regression; show the error and fail.

Fixes: 446c595dc0dd1759 ("perf test addr2line_inlines: Ensure inline information shows on LBR leaves")
Cc: Ian Rogers <irogers@google.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/shell/addr2line_inlines.sh | 23 ++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/tools/perf/tests/shell/addr2line_inlines.sh b/tools/perf/tests/shell/addr2line_inlines.sh
index e8754ef2d7f21ac2..ca88a6b886ca1ae3 100755
--- a/tools/perf/tests/shell/addr2line_inlines.sh
+++ b/tools/perf/tests/shell/addr2line_inlines.sh
@@ -8,6 +8,8 @@ err=0
 test_dir=$(mktemp -d /tmp/perf-test-inline-addr2line.XXXXXXXXXX)
 perf_data="${test_dir}/perf.data"
 perf_script_txt="${test_dir}/perf_script.txt"
+perf_record_err="${test_dir}/perf_record.err"
+lbr_skipped=0
 
 cleanup() {
     rm -rf "${test_dir}"
@@ -71,7 +73,22 @@ test_lbr() {
     fi
 
     # Record data. Currently only dwarf callchains support inlined functions.
-    perf record --call-graph lbr -e cycles:u -o "${perf_data}" -- perf test -w inlineloop 1
+    # Some PMUs advertise branches but reject branch sampling for cycles, e.g.
+    # AMD BRS, which only samples its own retired branches event.
+    if ! perf record --call-graph lbr -e cycles:u -o "${perf_data}" -- \
+            perf test -w inlineloop 1 2> "${perf_record_err}"
+    then
+        if grep -Fq "doesn't support branch stack sampling" "${perf_record_err}"
+        then
+            echo "Skip: LBR not supported by the PMU"
+            lbr_skipped=1
+        else
+            cat "${perf_record_err}"
+            echo "Inline unwinding lbr verification test [Failed to record]"
+            err=1
+        fi
+        return
+    fi
 
     # Check output with inline (default) and srcline
     perf script -i "${perf_data}" --fields +srcline > "${perf_script_txt}"
@@ -93,4 +110,8 @@ test_dwarf
 test_lbr
 
 cleanup
+if [ $err -eq 0 ] && [ $lbr_skipped -eq 1 ]
+then
+    exit 2
+fi
 exit $err
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 09/13] perf tests: Silence the libbfd probe in build-test
  2026-11-17 20:04 ` [PATCH v6 09/13] perf tests: Silence the libbfd probe in build-test Arnaldo Carvalho de Melo
@ 2026-11-17 20:08   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:08 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> build-test evaluates the libbfd probe at parse time and the compiler's
> stderr isn't captured by $(shell ...), so without binutils-devel it
> prints "bfd.h: No such file or directory" once per tests/make
> invocation, five times per run.
> 
> Redirect the compiler's stderr; the empty result still skips the
> nondistro tests as intended.
> 
> Fixes: 523471c5163659c6 ("perf build: Skip nondistro build test if libbfd is old")
> Reviewed-by: Ian Rogers <irogers@google.com>
> Cc: James Clark <james.clark@linaro.org>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=9


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 01/13] perf jvmti: Link the agent with EXTRA_CFLAGS
  2026-11-17 20:04 ` [PATCH v6 01/13] perf jvmti: Link the agent with EXTRA_CFLAGS Arnaldo Carvalho de Melo
@ 2026-11-17 20:10   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:10 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> The jvmti objects are compiled with EXTRA_CFLAGS but the agent is linked
> with LDFLAGS only, so flags with link-time parts never reach the shared
> object.  With -fprofile-instr-generate the agent's __profc counters had
> no __llvm_profile runtime to write them, and its files showed 0.00% in
> the coverage report.
> 
> Pass EXTRA_CFLAGS at link time, as the dlfilter rule already does.
> 
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=1


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 06/13] perf annotate-data: Count flexible array accesses for the whole struct
  2026-11-17 20:04 ` [PATCH v6 06/13] perf annotate-data: Count flexible array accesses for the whole struct Arnaldo Carvalho de Melo
@ 2026-11-17 20:10   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:10 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> The per-member percentage sums the histogram entries in the member's
> own [offset, offset + size) range.  A flexible array member is resolved
> past the end of its struct, so the struct row only covered its declared
> size and showed 0.00% while the member row showed 100.00% and the
> header said N samples.
> 
> Take a member's extent as the end of its last child as well, so the
> struct counts what was resolved past it.  Union members all start at
> offset 0, so a plain union keeps its size; a member that is itself a
> struct with a flexible array extends the union too.  The TUI browser
> accounts the members the same way, use the extent there as well.
> 
> Extend tests/shell/data_type_flex_array.sh to check that the accesses
> count for the struct in the percent, samples and period columns.
> 
> Fixes: aaca646915a1bb53 ("perf annotate-data: Adjust type offset for flex-array")
> Reviewed-by: Ian Rogers <irogers@google.com>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=6


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 08/13] perf annotate: Print the stats after the annotations
  2026-11-17 20:04 ` [PATCH v6 08/13] perf annotate: Print the stats after the annotations Arnaldo Carvalho de Melo
@ 2026-11-17 20:11   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:11 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> The instruction stats are printed from hists__find_annotations() before
> that event is annotated: the first event reports zeros and later events
> can report stale counts. The data type total is only counted in
> --data-type mode, so --type-stat without it is incomplete too.
> 
> Print the stats once after all events have been annotated, warning if
> --type-stat is used without --data-type. Leave the TUI's alternate
> screen before printing, so the stats remain on the terminal.
> 
> Fixes: 58824fa0087e1cb7 ("perf annotate: Add --insn-stat option for debugging")
> Fixes: 61a9741e9f78c64c ("perf annotate: Add --type-stat option for debugging")
> Cc: Namhyung Kim <namhyung@kernel.org>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=8


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 05/13] perf tests: Exercise the flexible array member path
  2026-11-17 20:04 ` [PATCH v6 05/13] perf tests: Exercise the flexible array member path Arnaldo Carvalho de Melo
@ 2026-11-17 20:11   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:11 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> No test covered find_flex_array() and get_flex_array_member().  Add a
> flex_array workload that keeps a struct with a trailing data[] behind a
> global pointer, and a test that checks the accesses past the struct size
> are resolved and named after the flexible array member; find_data_type()
> folds offsets past the struct modulo the element size back to the
> member's offset (+0x4 for char data[]).  The test skips on 32-bit PIE
> builds, which reach static globals relative to the GOT base register.
> 
> Reviewed-by: Ian Rogers <irogers@google.com>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=5


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 12/13] perf test: Drop samples without an IP in the dlfilter test
  2026-11-17 20:04 ` [PATCH v6 12/13] perf test: Drop samples without an IP in the dlfilter test Arnaldo Carvalho de Melo
@ 2026-11-17 20:12   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:12 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> Since v7.3 the kernel zeroes PERF_SAMPLE_IP when a userspace-only event
> skids into kernel mode, so that kernel register state is not exposed to
> an unprivileged event (166f10836a653dfa "perf/core: Fix kernel register
> info leak via hardware skid").
> 
> The test dlfilter keeps those samples, and the check that nothing but
> test_loop survives then trips on their lines: the comm column reads
> "perf".  Drop them, which is what the test wants, everything except
> test_loop must be filtered out.
> 
> Fixes: 199d5e872a9ce737 ("perf tests script dlfilter: Add a dlfilter test")
> Reviewed-by: Ian Rogers <irogers@google.com>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=12


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 04/13] perf tests: Exercise DW_OP_addrx global variables
  2026-11-17 20:04 ` [PATCH v6 04/13] perf tests: Exercise DW_OP_addrx global variables Arnaldo Carvalho de Melo
@ 2026-11-17 20:13   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:13 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> clang emits DW_AT_location with DW_OP_addrx for globals in DWARF 5, gcc
> emits DW_OP_addr.  The global_data workload keeps a zero initialized
> struct in .bss and the test checks that its accesses are annotated, so
> the DW_OP_addrx path is exercised when perf is built with clang and
> DWARF 5 (e.g. coverage.sh --dwarf-version 5), and the DW_OP_addr one
> with gcc.  It skips on 32-bit PIE builds, which reach static globals
> relative to the GOT base register, and the resolver does not track that.
> 
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=4


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 10/13] perf build: Record the display-only features in FEATURE-DUMP
  2026-11-17 20:04 ` [PATCH v6 10/13] perf build: Record the display-only features in FEATURE-DUMP Arnaldo Carvalho de Melo
@ 2026-11-17 20:15   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:15 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> llvm-perf and rust are in FEATURE_DISPLAY but not in FEATURE_TESTS: they
> can't be part of the test-all fast path, which sets every FEATURE_TESTS
> entry to 1 when test-all.c builds.  Since FEATURE-DUMP is written from
> FEATURE_TESTS only, a FEATURES_DUMP= build, as used by build-test, never
> sees them and silently builds without libLLVM and the rust workloads.
> 
> Append the display features that aren't in FEATURE_TESTS to the dump
> after the regular feature checks have run.
> 
> Fixes: 206dcfca1f5cfac3 ("perf build: Autodetect minimum required llvm-dev version")
> Fixes: 3f5dfa472ea6771c ("tools build: Fix rust feature detection")
> Cc: James Clark <james.clark@linaro.org>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=10


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 02/13] perf scripts: Add a coverage harness for perf
  2026-11-17 20:04 ` [PATCH v6 02/13] perf scripts: Add a coverage harness for perf Arnaldo Carvalho de Melo
@ 2026-11-17 20:15   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:15 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf scripts: sticky-bit and root-shell bypass in
  check_root_path()
- [Low] perf scripts: symlink following via chown without -h in
  fix_ownership()
--

commit a0647612ea142c35ebab221e54c45ab482f8b7ad
Author: Arnaldo Carvalho de Melo <acme@redhat.com>

perf scripts: Add a coverage harness for perf

This commit adds tools/perf/scripts/coverage.sh and corresponding make
targets to build perf with clang source-based coverage, run the perf test
suite, and report coverage statistics.

> diff --git a/tools/perf/scripts/coverage.sh b/tools/perf/scripts/coverage.sh
> new file mode 100755
> index 0000000000000..073d665229697
> --- /dev/null
> +++ b/tools/perf/scripts/coverage.sh
> @@ -0,0 +1,984 @@

[ ... ]

> +DEFAULT_BUILD_DIR=/tmp/build/perf-coverage

[ ... ]

> +if [ "$ROOT" = 1 ] && [ "$PREP" = 0 ] && [ "$TABLE" = 0 ]; then
> +	if [ "$REPORT_ONLY" = 0 ] && [ "$SKIP_BUILD" = 0 ] &&
> +	   { [ "$FORCE_BUILD" = 1 ] ||
> +	     { [ ! -f "$PERF_DIR/.coverage-build-dir" ] &&
> +	       [ ! -x "$BUILD_DIR/perf" ]; }; }; then
> +		say "Running as root, building perf in $BUILD_DIR"
> +	else
> +		SKIP_BUILD=1
> +		say "Running as root, using the coverage build in $BUILD_DIR"
> +	fi
> +fi
> +
> +#
> +# Under sudo the build directory comes from a file the invoking user wrote;
> +# root would execute the perf found there and delete and rewrite its cov/
> +# directory, so refuse one owned by somebody else or writable by another
> +# group or by others.  Every parent is checked too: a directory another
> +# user can write lets them swap an entry between this check and the run.
> +#
> +check_root_path()
> +{
> +	local path=$1 uid gid mode
> +	[ "$ROOT" = 1 ] && [ -n "${SUDO_UID:-}" ] || return 0
> +	# abspath() resolves links such as /home -> /var/home on Silverblue
> +	# and CoreOS, so the walk below checks their targets.  A path not
> +	# resolved that way, like $BUILD_DIR/perf, must not be a link.
> +	[ -L "$path" ] && die "$path is a symbolic link, use its target"
> +	while :; do
> +		if [ -e "$path" ]; then
> +			if ! read -r uid gid mode < \
> +				<(stat -c '%u %g %a' "$path" 2>/dev/null); then
> +				die "could not stat $path"
> +			fi
> +			# A sticky directory (/tmp) lets anybody create entries
> +			# but not replace one that belongs to somebody else.
> +			if [ $(( 8#$mode & 8#1000 )) = 0 ]; then
> +				[ "$uid" = 0 ] || [ "$uid" = "$SUDO_UID" ] ||
> +					die "$path is owned by uid $uid, not by root or $SUDO_UID"
> +				# A user private group with umask 002
> +				# gives 775, so only the developer's
> +				# own group may write here.
> +				[ $(( 8#$mode & 8#002 )) = 0 ] ||
> +					die "$path is writable by others"
> +				if [ $(( 8#$mode & 8#020 )) != 0 ] &&
> +				   [ "$gid" != "${SUDO_GID:-}" ]; then
> +					die "$path is writable by group $gid"
> +				fi
> +			fi
> +		fi
> +		[ "$path" = / ] && return 0
> +		path=$(dirname "$path")
> +	done
> +}

[Severity: Medium]
Can an unprivileged local user bypass check_root_path() by setting the
sticky bit on /tmp/build/perf-coverage?

Any user can chmod +t directories and files they own, and the sticky bit
check skips all uid, gid and permission checks when $(( 8#$mode & 8#1000 ))
is non-zero instead of only allowing root-owned sticky directories.
Because DEFAULT_BUILD_DIR is /tmp/build/perf-coverage and running as root
automatically sets SKIP_BUILD=1 whenever $BUILD_DIR/perf is executable, a
local user can pre-create /tmp/build/perf-coverage/perf with mode 1755 (and
a dummy __llvm_profile symbol) so that root executes their binary.  In
addition, an owner of a sticky /tmp/build directory can still unlink and
replace subdirectories inside it, and check_root_path() returns early when
SUDO_UID is unset in a direct root shell.

[ ... ]

> +fix_ownership()
> +{
> +	local uid gid path
> +	[ "$ROOT" = 1 ] || return 0
> +	if [ -n "${SUDO_UID:-}" ]; then
> +		uid=$SUDO_UID
> +		gid=${SUDO_GID:-$SUDO_UID}
> +	else
> +		read -r uid gid < <(stat -c '%u %g' "$PERF_DIR" 2>/dev/null) || return 0
> +	fi
> +	[ "$uid" != 0 ] || return 0
> +
> +	for path in "${COV_WRITES[@]}"; do
> +		[ -e "$COV_DIR/$path" ] || continue
> +		chown -R "$uid:$gid" "$COV_DIR/$path" ||
> +			warn "could not give $COV_DIR/$path back to uid $uid"
> +	done
> +	if [ "$COV_CREATED" = 1 ]; then
> +		chown "$uid:$gid" "$COV_DIR" ||
> +			warn "could not give $COV_DIR back to uid $uid"
> +	fi
> +	if [ "$BUILD_CREATED" = 1 ]; then
> +		chown -R "$uid:$gid" "$BUILD_DIR" ||
> +			warn "could not give $BUILD_DIR back to uid $uid"
> +	fi
> +	if [ -n "$BUILD_STAMP" ] && [ -f "$BUILD_STAMP" ]; then
> +		# -h: find matches a symlink by its own owner, but chown would
> +		# otherwise follow it and change a file outside the build tree.
> +		find "$BUILD_DIR" -user 0 -newer "$BUILD_STAMP" \
> +			-exec chown -h "$uid:$gid" {} + ||
> +			warn "could not give the files root built in $BUILD_DIR back to uid $uid"
> +		rm -f "$BUILD_STAMP" || true
> +	fi
> +	if [ "$STATE_DIR_CREATED" = 1 ] && [ -d "$STATE_DIR" ]; then
> +		chown "$uid:$gid" "$STATE_DIR" ||
> +			warn "could not give $STATE_DIR back to uid $uid"
> +	fi
> +	if [ -f "$STATE_FILE" ]; then
> +		chown "$uid:$gid" "$STATE_FILE" ||
> +			warn "could not give $STATE_FILE back to uid $uid"
> +	fi
> +	if [ -f "$POINTER" ]; then
> +		chown "$uid:$gid" "$POINTER" ||
> +			warn "could not give $POINTER back to uid $uid"
> +	fi
> +}

[Severity: Low]
Should the other chown calls in fix_ownership() also pass -h (and -RP when
recursive)?

Unlike the find ... -exec chown -h call on $BUILD_DIR, the chown calls on
$COV_DIR/$path, $STATE_FILE and $POINTER do not pass -h, and [ -e ] / [ -f ]
succeed on symlinks to existing files.  If any of those paths is a symlink
(for example during --report-only, a subset -t run where baseline.txt is
not overwritten, or an early exit after the EXIT trap is armed), root will
dereference the symlink and hand ownership of its target to $uid:$gid.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=2

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 11/13] perf tests: Fix the Intel PT virtual LBR check
  2026-11-17 20:04 ` [PATCH v6 11/13] perf tests: Fix the Intel PT virtual LBR check Arnaldo Carvalho de Melo
@ 2026-11-17 20:18   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:18 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> With one data page the virtual LBR record loses its AUX samples:
> 
>   Warning: Processed 29 samples and lost 100.00%
> 
> Use --per-thread with 4 data and 64 AUX pages. Its single 276 KiB
> buffer (including metadata on 4 KiB pages) fits the default 516 KiB
> mlock allowance, regardless of CPU count. Also require the Python perf
> module to export session: importing an unrelated perf module used
> to fail the check instead of skipping it.
> 
> Fixes: 915d6b14dc0c ("perf test: Migrate Intel PT virtual LBR test to Python API")
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=11


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 07/13] perf tests: Exercise data type resolution from a register
  2026-11-17 20:04 ` [PATCH v6 07/13] perf tests: Exercise data type resolution from a register Arnaldo Carvalho de Melo
@ 2026-11-17 20:20   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> At -O2 a struct pointer lives in a register (with DWARF describing it with
> a register location on register-argument ABIs), while the unoptimized
> workloads the data type tests profile keep their variables on the stack.
> 
> Add a regptr workload with a noinline function that takes the pointer from
> a volatile global, so the compiler cannot constant-propagate the object
> address, and check that its three member accesses are resolved from that
> register.
> 
> Reviewed-by: Ian Rogers <irogers@google.com>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=7


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 13/13] perf test: Skip the LBR inline unwinding check when unsupported
  2026-11-17 20:04 ` [PATCH v6 13/13] perf test: Skip the LBR inline unwinding check when unsupported Arnaldo Carvalho de Melo
@ 2026-11-17 20:29   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:29 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf test: test_lbr() fails to skip on AMD BRS due to EINVAL
  vs EOPNOTSUPP
--

commit d21ae617447c59168b264906c4d199925c28a698
Author: Arnaldo Carvalho de Melo <acme@redhat.com>

perf test: Skip the LBR inline unwinding check when unsupported

This commit updates test_lbr() in addr2line_inlines.sh to skip the LBR
test when perf record reports that the PMU does not support branch stack
sampling.

> diff --git a/tools/perf/tests/shell/addr2line_inlines.sh b/tools/perf/tests/shell/addr2line_inlines.sh
> index e8754ef2d7f21..ca88a6b886ca1 100755
> --- a/tools/perf/tests/shell/addr2line_inlines.sh
> +++ b/tools/perf/tests/shell/addr2line_inlines.sh
[ ... ]
> @@ -71,7 +73,22 @@ test_lbr() {
>      fi
>  
>      # Record data. Currently only dwarf callchains support inlined functions.
> -    perf record --call-graph lbr -e cycles:u -o "${perf_data}" -- perf test -w inlineloop 1
> +    # Some PMUs advertise branches but reject branch sampling for cycles, e.g.
> +    # AMD BRS, which only samples its own retired branches event.
> +    if ! perf record --call-graph lbr -e cycles:u -o "${perf_data}" -- \
> +            perf test -w inlineloop 1 2> "${perf_record_err}"
> +    then
> +        if grep -Fq "doesn't support branch stack sampling" "${perf_record_err}"
> +        then
> +            echo "Skip: LBR not supported by the PMU"
> +            lbr_skipped=1
> +        else
> +            cat "${perf_record_err}"
> +            echo "Inline unwinding lbr verification test [Failed to record]"
> +            err=1
> +        fi
> +        return
> +    fi

[Severity: Medium]
Does test_lbr() still fail on AMD BRS instead of skipping?

On AMD BRS, amd_brs_hw_config() rejects non-BRS events such as cycles:u
with -EINVAL rather than -EOPNOTSUPP:

arch/x86/events/amd/brs.c:amd_brs_hw_config() {
    ...
	if (!amd_is_brs_event(event))
		return -EINVAL;
    ...
}

In tools/perf/util/evsel.c, evsel__open_strerror() only formats the
"doesn't support branch stack sampling" message under case EOPNOTSUPP, and
falls through to the generic Invalid argument message for EINVAL.  As a
result, grep does not match perf_record_err on AMD BRS and test_lbr() still
enters the else branch and fails with err=1.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=13

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v6 03/13] perf annotate: Handle absolute addressing in data type profiling
  2026-11-17 20:04 ` [PATCH v6 03/13] perf annotate: Handle absolute addressing in data type profiling Arnaldo Carvalho de Melo
@ 2026-11-17 20:35   ` sashiko-bot
  0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-11-17 20:35 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users

> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> 
> 32-bit x86 accesses globals with absolute addresses, like
> "movl %eax,0x8c73448", but the operand parser only recognized the
> offset(reg) forms, so the operand had no memory reference, the resolver
> saw a zero data address and resolved nothing.
> 
> Parse a bare "0x" number as an absolute memory operand, leaving jumps,
> calls, loop* and the 64-bit movabs* out, use the address in the resolver,
> and let the instruction tracking record the type of an absolute global
> load in the destination register.  The displacement is sign extended for
> 64-bit objects, as the CPU does, and zero extended for 32-bit ones.
> Values that do not fit the int offset, zero, and the operands of
> relocatable objects, which are unrelocated addends, are skipped.
> 
> With bare numbers being memory operands, a frame base register of -1,
> "none", would also compare equal to the -1 "no register" of an absolute
> operand and take it for a stack access, so guard those comparisons.
> Operands left with no register, like a user space "%gs:imm", are no
> longer looked up as variables, which can change the data type stats of
> those samples.
> 
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=3


^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2026-11-17 20:35 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
2026-11-17 20:04 ` [PATCH v6 01/13] perf jvmti: Link the agent with EXTRA_CFLAGS Arnaldo Carvalho de Melo
2026-11-17 20:10   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 02/13] perf scripts: Add a coverage harness for perf Arnaldo Carvalho de Melo
2026-11-17 20:15   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 03/13] perf annotate: Handle absolute addressing in data type profiling Arnaldo Carvalho de Melo
2026-11-17 20:35   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 04/13] perf tests: Exercise DW_OP_addrx global variables Arnaldo Carvalho de Melo
2026-11-17 20:13   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 05/13] perf tests: Exercise the flexible array member path Arnaldo Carvalho de Melo
2026-11-17 20:11   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 06/13] perf annotate-data: Count flexible array accesses for the whole struct Arnaldo Carvalho de Melo
2026-11-17 20:10   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 07/13] perf tests: Exercise data type resolution from a register Arnaldo Carvalho de Melo
2026-11-17 20:20   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 08/13] perf annotate: Print the stats after the annotations Arnaldo Carvalho de Melo
2026-11-17 20:11   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 09/13] perf tests: Silence the libbfd probe in build-test Arnaldo Carvalho de Melo
2026-11-17 20:08   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 10/13] perf build: Record the display-only features in FEATURE-DUMP Arnaldo Carvalho de Melo
2026-11-17 20:15   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 11/13] perf tests: Fix the Intel PT virtual LBR check Arnaldo Carvalho de Melo
2026-11-17 20:18   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 12/13] perf test: Drop samples without an IP in the dlfilter test Arnaldo Carvalho de Melo
2026-11-17 20:12   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 13/13] perf test: Skip the LBR inline unwinding check when unsupported Arnaldo Carvalho de Melo
2026-11-17 20:29   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox