Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH v4 0/7] perf synthetic-events: Use PROCMAP_QUERY for mmap synthesis
@ 2026-12-04 20:58 Ian Rogers
  2026-12-04 20:58 ` [PATCH v4 1/7] perf machine: Fix leak when creating host kernel maps fails Ian Rogers
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: Ian Rogers @ 2026-12-04 20:58 UTC (permalink / raw)
  To: linux-perf-users
  Cc: linux-kernel, peterz, mingo, acme, namhyung, jolsa, adrian.hunter,
	james.clark, dsahern, andrii, Ian Rogers

Today perf parses the text of /proc/<pid>/task/<pid>/maps and, for
each file backed mapping missing from the DSO cache, opens and parses
the ELF file, within the process' mount namespace, for its build ID.
PROCMAP_QUERY (Linux 6.11+, used here on Linux 6.19+) returns each VMA
in binary form, can filter VMAs by permission in the kernel, and
returns the build ID read by the kernel from the mapped file. This
works for deleted binaries and binaries in other mount namespaces,
where perf currently gets no build ID.

While PROCMAP_QUERY was being developed, Andrii Nakryiko prototyped
using it for perf's mmap synthesis and measured it with 'perf bench
internals synthesize', finding it slightly faster than parsing the
text, but the perf change wasn't posted for inclusion [1][2]. This
series differs in letting the kernel filter VMAs, in getting build IDs
from the kernel, as build IDs in synthesized MMAP2 events have since
become the default, and in fixing the benchmark, which was failing to
read build IDs (patch 3).

[1] https://lore.kernel.org/all/CAEf4BzbdGJzMuRgGJE72VFquXL37rS9Ti__wx4f_+kt3yetkEg@mail.gmail.com/
[2] https://github.com/anakryiko/linux/commit/0841fe675ed30f5605c5b228e18f5612ea253b35

The series is based on perf-tools-next.

Patches 1 and 2 fix pre-existing bugs found by the Sashiko AI review
tool when reviewing an earlier version of this series. Patch 1 fixes a
leak in __machine__new_host() when creating the kernel maps fails.
Patch 2 fixes a data race on DSO build IDs when several threads
synthesize events, by reading and setting the build ID with the dsos
lock held, as lookups that compare DSO ids already do. ThreadSanitizer
reported 8 races on the build ID in 'perf bench internals synthesize
-t -m 8 -M 8' before the fix and none after it.

Patch 3 fixes 'perf bench internals synthesize', which never called
symbol__init() so every build ID read failed and the DSO cache never
worked, making the benchmark ~15x slower than real synthesis.

Patch 4 synchronizes tools/include/uapi/linux/fs.h with
include/uapi/linux/fs.h and tracks it in tools/perf/check-headers.sh.

Patch 5 adds the PROCMAP_QUERY path, gated on Linux 6.19+ so both
per-VMA locks (6.18) and safe __kernel_read() build ID reads (6.19)
are present. Without data mmaps the kernel returns only executable
VMAs. With data mmaps (readable or executable VMAs are wanted) a first
pass gets readable VMAs, skipping guard pages, and a second pass gets
executable VMAs that aren't readable, such as execute-only memory. The
DSO cache is consulted first (keyed by device/inode as before), and
only on a miss is the VMA queried again for the kernel to read the
build ID, so each file's build ID is read once. If the kernel can't
give a build ID, the file is read as before, with the existing nsinfo
handling; no nsinfo or mount namespace work is done when the kernel or
DSO cache provides the build ID. The text parser remains as a fallback
on kernels older than 6.19, when the ioctl isn't supported (probed
once with pthread_once()), or when the first query fails. Shared code
is in a helper so both paths emit identical events.

Patch 6 adds a 'perf bench internals synthesize --[no-]procmap-query'
option, a core.procmap-query config option to allow comparison, and
exports perf_event__procmap_query_supported().

Patch 7 adds a perf test that synthesizes the test process' mmaps both
ways and checks the MMAP2 events are byte for byte identical, with and
without build IDs and data mmaps, including an execute-only mapping.

Results on an Intel Core i7-14700, 7.1 kernel (usec, lower is better).

 Single threaded, perf process, warm DSO cache, pinned to a P-core CPU,
 min / median of 10 interleaved runs:
                        text              PROCMAP_QUERY
   exec mmaps only    254.1 /  256.9      171.1 /  173.5   (-32%)
   with data mmaps   1513.4 / 1618.1     1667.8 / 1749.1   (+8%)

 System wide ('-t -m 1 -M 4', ~24,000 events, unprivileged), cold
 DSO cache as at the start of 'perf record -a', min / median of 5
 interleaved runs:
   threads              text              PROCMAP_QUERY
     1               243754 / 246155     124929 / 126336   (-49%)
     2               139272 / 143913      79233 /  80165   (-44%)
     3               207429 / 209567      93290 /  94799   (-55%)
     4               123863 / 123989      59701 /  62360   (-50%)

PROCMAP_QUERY is not a win everywhere: with data mmaps and a warm DSO
cache, one ioctl per readable VMA is slightly slower than formatting
and parsing the text. Splitting data mmap synthesis into the readable
and execute-only passes avoids querying the ~22% of VMAs that are
inaccessible guards, cutting the ioctls when walking every process on
the test machine by 13%, but the time only improved by about 1% as
those VMAs have no name and are cheap to query. The benefit for
executable mmaps, the default for 'perf record', comes from the kernel
filtering VMAs and from not opening ELF files.

Recording with and without core.procmap-query on 'sleep' and a
python3 process with many libraries, with and without -d, gave
identical mmap events in 'perf script --show-mmap-events'. For a
process running a deleted binary, PROCMAP_QUERY gave the build ID
where the text path only had the device/inode.

Known difference: the gate VMA ([vsyscall] on x86-64, [vectors] on
32-bit ARM) isn't in the VMA tree, so no map is synthesized for it.
No symbols were ever resolved for it, as it has no backing file, and
samples there are now shown as [unknown] rather than [vsyscall] or
[vectors]. The kernel never emits an MMAP2 event for the gate VMA, so
processes started during recording already lacked it; PROCMAP_QUERY
makes pre-existing processes consistent with them.

Tools used: the code, tests and changelogs in this series were
initially written by an AI coding assistant (Gemini in Antigravity)
from prompts describing the goal above, and were then reviewed,
built (GCC and clang), tested ('perf test', the new equivalence
test, 'perf record'/'perf script' comparisons) and benchmarked by
me.

v4:
- Address Sashiko AI review feedback on v3 [5]:
  - Gate all PROCMAP_QUERY synthesis (not just build ID queries) on
    Linux 6.19+ in procmap_query__probe(), avoiding both the
    data-mmap regression without kernel build IDs on 6.11-6.18 and
    the mmap_lock contention regression on 6.17 (where text maps had
    per-VMA locks via commit 5631da56c9a8 before PROCMAP_QUERY gained
    them in 6.18 via commit d9d1c2d81797).
  - Export perf_event__procmap_query_supported() in patch 6 so
    'perf bench internals synthesize' prints the actual mode and
    patch 7's test reuses the same probe rather than duplicating it.

v3:
- Address Sashiko AI review feedback on v2 [4]:
  - Add patch 4 synchronizing tools/include/uapi/linux/fs.h with
    include/uapi/linux/fs.h and tracking it in
    tools/perf/check-headers.sh; fix the <linux/fs.h> include comment
    and commit message in patch 5.
  - Gate kernel PROCMAP_QUERY build ID queries on Linux 6.19+ (checked
    via uname() in procmap_query__probe()) so perf never triggers a
    kernel NULL pointer dereference in filemap_read_folio() on
    6.11-6.18 kernels lacking commit 777a8560fd29 for mappings without
    ->read_folio (such as tmpfs, shmem, hugetlbfs, dma-buf, or devices).
  - Tighten procmap_query__skip_build_id() to also skip /dev/shm/,
    /memfd:, /dmabuf:, /anon_hugepage, /SYSV, and /[ pseudo-paths on
    6.19+ kernels.

v2:
- Address Sashiko AI review feedback on patch 4 [3]:
  - Skip device nodes (except /dev/shm), pseudo-filesystems (/sys,
    /proc), and other visible non-regular files before asking the kernel
    for a build ID, avoiding __kernel_read() warnings on device files
    and a kernel NULL pointer dereference on 6.11-6.18 kernels lacking
    commit 777a8560fd29.
  - Keep addr non-decreasing when retrying Pass 2 queries for VMA names
    (and guard against non-advancing vma_end) to prevent an infinite
    loop if a target concurrently mprotect()s a VMA prefix.
  - Check --proc-map-timeout while skipping readable VMAs in Pass 2 so
    timeout warnings and PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT are
    still emitted.

[3] https://lore.kernel.org/linux-perf-users/20261204010452.1937224-1-irogers@google.com/
[4] https://lore.kernel.org/linux-perf-users/20261204045948.2254888-1-irogers@google.com/
[5] https://lore.kernel.org/linux-perf-users/20261204062328.2508164-1-irogers@google.com/

Ian Rogers (7):
  perf machine: Fix leak when creating host kernel maps fails
  perf synthetic-events: Fix race on DSO build IDs when synthesizing
  perf bench synthesize: Initialize symbols so build IDs can be read
  tools headers UAPI: Synchronize linux/fs.h with the kernel sources
  perf synthetic-events: Use PROCMAP_QUERY to synthesize mmap events
  perf bench synthesize: Add --procmap-query option
  perf test: Add test comparing PROCMAP_QUERY and /proc/pid/maps
    synthesis

 tools/include/uapi/linux/fs.h            | 137 ++++-
 tools/perf/Documentation/perf-bench.txt  |   4 +-
 tools/perf/Documentation/perf-config.txt |   6 +
 tools/perf/bench/synthesize.c            |  59 ++-
 tools/perf/check-headers.sh              |   1 +
 tools/perf/tests/Build                   |   1 +
 tools/perf/tests/builtin-test.c          |   1 +
 tools/perf/tests/synthesize-mmap.c       | 257 ++++++++++
 tools/perf/tests/tests.h                 |   1 +
 tools/perf/util/config.c                 |   4 +
 tools/perf/util/dsos.c                   |  38 +-
 tools/perf/util/dsos.h                   |   4 +
 tools/perf/util/machine.c                |   2 +-
 tools/perf/util/synthetic-events.c       | 610 +++++++++++++++++++----
 tools/perf/util/synthetic-events.h       |   7 +
 15 files changed, 993 insertions(+), 139 deletions(-)
 create mode 100644 tools/perf/tests/synthesize-mmap.c


base-commit: 22f7d78dc87e43ff4f7c03d23f46f49692c41ad0
-- 
2.56.1.915.g69d5ec7298-goog

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

end of thread, other threads:[~2026-12-04 21:16 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 20:58 [PATCH v4 0/7] perf synthetic-events: Use PROCMAP_QUERY for mmap synthesis Ian Rogers
2026-12-04 20:58 ` [PATCH v4 1/7] perf machine: Fix leak when creating host kernel maps fails Ian Rogers
2026-12-04 20:58 ` [PATCH v4 2/7] perf synthetic-events: Fix race on DSO build IDs when synthesizing Ian Rogers
2026-12-04 20:58 ` [PATCH v4 3/7] perf bench synthesize: Initialize symbols so build IDs can be read Ian Rogers
2026-12-04 20:58 ` [PATCH v4 4/7] tools headers UAPI: Synchronize linux/fs.h with the kernel sources Ian Rogers
2026-12-04 20:58 ` [PATCH v4 5/7] perf synthetic-events: Use PROCMAP_QUERY to synthesize mmap events Ian Rogers
2026-12-04 20:58 ` [PATCH v4 6/7] perf bench synthesize: Add --procmap-query option Ian Rogers
2026-12-04 20:58 ` [PATCH v4 7/7] perf test: Add test comparing PROCMAP_QUERY and /proc/pid/maps synthesis Ian Rogers
2026-12-04 21:16   ` sashiko-bot
2026-12-04 21:16 ` [PATCH v4 0/7] perf synthetic-events: Use PROCMAP_QUERY for mmap synthesis sashiko-bot

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