From: Ian Rogers <irogers@google.com>
To: linux-perf-users@vger.kernel.org, acme@kernel.org, namhyung@kernel.org
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
mingo@redhat.com, jolsa@kernel.org, adrian.hunter@intel.com,
james.clark@linaro.org, dsahern@gmail.com, andrii@kernel.org,
Ian Rogers <irogers@google.com>
Subject: [PATCH v5 7/7] perf test: Add test comparing PROCMAP_QUERY and /proc/pid/maps synthesis
Date: Sat, 5 Dec 2026 18:34:13 -0800 [thread overview]
Message-ID: <20261206023413.3078292-8-irogers@google.com> (raw)
In-Reply-To: <20261206023413.3078292-1-irogers@google.com>
Synthesize the MMAP2 events of a frozen child process both by parsing
/proc/<pid>/maps and with the PROCMAP_QUERY ioctl, and check the events
are byte for byte identical, including the build ID or device/inode
fields, the filename padding and a 3 u64 id header trailer. The child
immediately stops itself with SIGSTOP (with PR_SET_PDEATHSIG set to
SIGKILL) so its VMA tree cannot be mutated by the parent's allocations,
ELF mmaps, or lazy library loads while the two synthesis paths run;
this lets all VMAs, including anonymous, heap, stack, and vDSO
mappings, be compared byte for byte. Only the gate VMA, such as
[vsyscall] or [vectors], is ignored because it is only in the maps file
and not in the VMA tree. The event buffer is filled with a different
byte pattern for each synthesis, so any byte not written by synthesis
causes a mismatch. Each synthesis uses a new machine initialized with
machine__init() so that the DSO build ID cache isn't shared without
repeating kernel map setup. proc_map_timeout is temporarily raised so
load spikes don't cause timeout truncation mismatches. The comparison
is done with and without build IDs and with and without data mmaps. The
events are sorted by address before being compared, as with data mmaps
PROCMAP_QUERY finds execute-only VMAs after the readable ones. To cover
the execute-only case, a page of the perf binary is mapped with only
PROT_EXEC before fork() and the test checks both paths have an event
for it. The test is skipped if PROCMAP_QUERY synthesis isn't supported
on the running kernel.
$ perf test -vv PROCMAP 2>&1 | grep -E 'Compared|PROCMAP'
30: Synthesize MMAP2 events with PROCMAP_QUERY:
Compared 62 MMAP2 events (mmap_data=0, build IDs=1)
Compared 332 MMAP2 events (mmap_data=1, build IDs=1)
Compared 62 MMAP2 events (mmap_data=0, build IDs=0)
Compared 332 MMAP2 events (mmap_data=1, build IDs=0)
30: Synthesize MMAP2 events with PROCMAP_QUERY : Ok
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/Build | 1 +
tools/perf/tests/builtin-test.c | 1 +
tools/perf/tests/synthesize-mmap.c | 300 +++++++++++++++++++++++++++++
tools/perf/tests/tests.h | 1 +
4 files changed, 303 insertions(+)
create mode 100644 tools/perf/tests/synthesize-mmap.c
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index a95ca6cbcb6d..ce36ee23a5e9 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -29,6 +29,7 @@ perf-test-y += wp.o
perf-test-y += task-exit.o
perf-test-y += sw-clock.o
perf-test-y += mmap-thread-lookup.o
+perf-test-y += synthesize-mmap.o
perf-test-y += thread-maps-share.o
perf-test-y += thread-stack.o
perf-test-$(CONFIG_LIBTRACEEVENT) += switch-tracking.o
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index f88e4ad8a319..859b2b70b768 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -145,6 +145,7 @@ static struct test_suite *generic_tests[] = {
&suite__parse_no_sample_id_all,
&suite__hists_filter,
&suite__mmap_thread_lookup,
+ &suite__synthesize_procmap_query,
&suite__thread_maps_share,
&suite__thread_stack,
&suite__hists_output,
diff --git a/tools/perf/tests/synthesize-mmap.c b/tools/perf/tests/synthesize-mmap.c
new file mode 100644
index 000000000000..70f90124a33c
--- /dev/null
+++ b/tools/perf/tests/synthesize-mmap.c
@@ -0,0 +1,300 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Check that synthesizing MMAP2 events using the PROCMAP_QUERY ioctl gives the
+ * same events as parsing the text of /proc/<pid>/maps.
+ */
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/zalloc.h>
+#include "debug.h"
+#include "event.h"
+#include "machine.h"
+#include "symbol_conf.h"
+#include "synthetic-events.h"
+#include "tests.h"
+#include "tool.h"
+
+struct synth_events {
+ struct perf_tool tool;
+ union perf_event **events;
+ size_t nr;
+ size_t alloc;
+};
+
+static int process_event(const struct perf_tool *tool, union perf_event *event,
+ struct perf_sample *sample __maybe_unused,
+ struct machine *machine __maybe_unused)
+{
+ struct synth_events *se = container_of(tool, struct synth_events, tool);
+ union perf_event *copy;
+
+ if (se->nr == se->alloc) {
+ size_t alloc = se->alloc ? se->alloc * 2 : 64;
+ union perf_event **events = realloc(se->events, alloc * sizeof(*events));
+
+ if (!events)
+ return -ENOMEM;
+ se->events = events;
+ se->alloc = alloc;
+ }
+ copy = memdup(event, event->header.size);
+ if (!copy)
+ return -ENOMEM;
+ se->events[se->nr++] = copy;
+ return 0;
+}
+
+static void synth_events__exit(struct synth_events *se)
+{
+ for (size_t i = 0; i < se->nr; i++)
+ free(se->events[i]);
+ zfree(&se->events);
+ se->nr = 0;
+ se->alloc = 0;
+}
+
+/*
+ * An id header trailer as when sample_id_all is set, so that its zeroing is
+ * compared too.
+ */
+#define TEST_ID_HDR_SIZE (3 * sizeof(u64))
+
+/*
+ * Synthesize pid's MMAP2 events. The event buffer is filled with fill first so
+ * that any byte of an event not written by synthesis, such as padding, differs
+ * between the two synthesis runs and fails the comparison.
+ */
+static int synthesize(struct synth_events *se, pid_t pid, bool procmap_query,
+ bool mmap_data, int fill)
+{
+ bool saved_procmap_query = synthesize_procmap_query;
+ struct machine machine;
+ union perf_event *event;
+ int err = -1;
+
+ perf_tool__init(&se->tool, /*ordered_events=*/false);
+ /* Use a new machine so DSO build ID caches aren't shared. */
+ if (machine__init(&machine, "", HOST_KERNEL_ID) != 0)
+ return -1;
+ machine.id_hdr_size = TEST_ID_HDR_SIZE;
+
+ event = malloc(sizeof(event->mmap2) + machine.id_hdr_size);
+ if (!event)
+ goto out_machine;
+ memset(event, fill, sizeof(event->mmap2) + machine.id_hdr_size);
+
+ synthesize_procmap_query = procmap_query;
+ err = perf_event__synthesize_mmap_events(&se->tool, event, pid, pid,
+ process_event, &machine, mmap_data);
+ synthesize_procmap_query = saved_procmap_query;
+
+ free(event);
+out_machine:
+ machine__exit(&machine);
+ return err;
+}
+
+static void print_event(const char *prefix, union perf_event *event)
+{
+ pr_debug("%s: ", prefix);
+ if (verbose > 0)
+ perf_event__fprintf_mmap2(event, debug_file());
+}
+
+/*
+ * The gate VMA, such as [vsyscall] on x86-64 or [vectors] on 32-bit ARM, isn't
+ * in the process' VMA tree; it is shown in /proc/<pid>/maps but not reported
+ * by PROCMAP_QUERY, so it is ignored.
+ */
+static bool is_gate_vma(union perf_event *event)
+{
+ const char *name = event->mmap2.filename;
+
+ return !strcmp(name, "[vsyscall]") || !strcmp(name, "[vectors]");
+}
+
+static int cmp_event(const void *a, const void *b)
+{
+ const union perf_event *ea = *(const union perf_event * const *)a;
+ const union perf_event *eb = *(const union perf_event * const *)b;
+
+ if (ea->mmap2.start != eb->mmap2.start)
+ return ea->mmap2.start < eb->mmap2.start ? -1 : 1;
+ if (ea->header.size != eb->header.size)
+ return ea->header.size < eb->header.size ? -1 : 1;
+ return memcmp(ea, eb, ea->header.size);
+}
+
+/* Is there an event for a mapping at addr? */
+static bool has_mapping(const struct synth_events *se, u64 addr)
+{
+ for (size_t i = 0; i < se->nr; i++) {
+ if (se->events[i]->mmap2.start == addr)
+ return true;
+ }
+ return false;
+}
+
+static int compare(struct synth_events *text, struct synth_events *query, bool mmap_data)
+{
+ size_t i = 0, j = 0, compared = 0;
+ int ret = TEST_OK;
+
+ /*
+ * With data mmaps PROCMAP_QUERY finds the readable VMAs before the
+ * execute-only ones, so order the events by address.
+ */
+ qsort(text->events, text->nr, sizeof(*text->events), cmp_event);
+ qsort(query->events, query->nr, sizeof(*query->events), cmp_event);
+
+ while (true) {
+ while (i < text->nr && is_gate_vma(text->events[i]))
+ i++;
+ while (j < query->nr && is_gate_vma(query->events[j]))
+ j++;
+ if (i == text->nr || j == query->nr)
+ break;
+
+ if (text->events[i]->header.size != query->events[j]->header.size ||
+ memcmp(text->events[i], query->events[j], text->events[i]->header.size)) {
+ pr_debug("MMAP2 event mismatch\n");
+ print_event("text", text->events[i]);
+ print_event("procmap_query", query->events[j]);
+ ret = TEST_FAIL;
+ }
+ compared++;
+ i++;
+ j++;
+ }
+ if (i != text->nr || j != query->nr) {
+ pr_debug("Number of MMAP2 events differs: text %zu procmap_query %zu\n",
+ text->nr, query->nr);
+ ret = TEST_FAIL;
+ }
+ if (compared == 0) {
+ pr_debug("No MMAP2 events compared\n");
+ ret = TEST_FAIL;
+ }
+ pr_debug("Compared %zu MMAP2 events (mmap_data=%d, build IDs=%d)\n",
+ compared, mmap_data, !symbol_conf.no_buildid_mmap2);
+ return ret;
+}
+
+/*
+ * Fork a child that immediately stops itself so its VMA tree is frozen while
+ * the parent runs both synthesis paths against it. This prevents the parent's
+ * own allocations, ELF mmaps, or lazy library loading from mutating the target
+ * process' mappings mid-test.
+ */
+static pid_t spawn_stopped_child(void)
+{
+ pid_t parent = getpid();
+ pid_t child;
+ int status;
+
+ child = fork();
+ if (child < 0)
+ return -1;
+ if (child == 0) {
+ if (prctl(PR_SET_PDEATHSIG, SIGKILL) == 0 && getppid() == parent)
+ raise(SIGSTOP);
+ _exit(0);
+ }
+
+ while (waitpid(child, &status, WUNTRACED) < 0) {
+ if (errno != EINTR) {
+ kill(child, SIGKILL);
+ while (waitpid(child, NULL, 0) < 0 && errno == EINTR)
+ ;
+ return -1;
+ }
+ }
+ if (!WIFSTOPPED(status))
+ return -1;
+
+ return child;
+}
+
+static int test__synthesize_procmap_query(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ bool saved_no_buildid_mmap2 = symbol_conf.no_buildid_mmap2;
+ unsigned int saved_proc_map_timeout = proc_map_timeout;
+ long page_size = sysconf(_SC_PAGESIZE);
+ int ret = TEST_OK;
+ pid_t child;
+ void *xonly;
+ int fd;
+
+ if (!perf_event__procmap_query_supported()) {
+ pr_debug("PROCMAP_QUERY not supported\n");
+ return TEST_SKIP;
+ }
+
+ /*
+ * Add an execute-only file mapping, that is found by a second
+ * PROCMAP_QUERY pass when synthesizing data mmaps. Some
+ * architectures may still report it as readable.
+ */
+ fd = open("/proc/self/exe", O_RDONLY);
+ if (fd < 0) {
+ pr_debug("Failed to open /proc/self/exe\n");
+ return TEST_FAIL;
+ }
+ xonly = mmap(NULL, page_size, PROT_EXEC, MAP_PRIVATE, fd, 0);
+ close(fd);
+ if (xonly == MAP_FAILED) {
+ pr_debug("Failed to mmap an execute-only page\n");
+ return TEST_FAIL;
+ }
+
+ child = spawn_stopped_child();
+ if (child < 0) {
+ pr_debug("Failed to spawn stopped child\n");
+ munmap(xonly, page_size);
+ return TEST_FAIL;
+ }
+
+ /* Avoid timeout truncation mismatches on heavily loaded systems. */
+ proc_map_timeout = 60000;
+ for (int i = 0; i < 4 && ret == TEST_OK; i++) {
+ bool mmap_data = i & 1;
+ struct synth_events text = {}, query = {};
+
+ symbol_conf.no_buildid_mmap2 = i & 2;
+ if (synthesize(&text, child, /*procmap_query=*/false, mmap_data, /*fill=*/0xaa) ||
+ synthesize(&query, child, /*procmap_query=*/true, mmap_data, /*fill=*/0x55)) {
+ pr_debug("Synthesizing MMAP2 events failed\n");
+ ret = TEST_FAIL;
+ } else if (!has_mapping(&text, (u64)(uintptr_t)xonly) ||
+ !has_mapping(&query, (u64)(uintptr_t)xonly)) {
+ pr_debug("Missing execute-only mapping at %p\n", xonly);
+ ret = TEST_FAIL;
+ } else {
+ ret = compare(&text, &query, mmap_data);
+ }
+ synth_events__exit(&text);
+ synth_events__exit(&query);
+ }
+ proc_map_timeout = saved_proc_map_timeout;
+ symbol_conf.no_buildid_mmap2 = saved_no_buildid_mmap2;
+
+ kill(child, SIGKILL);
+ while (waitpid(child, NULL, 0) < 0 && errno == EINTR)
+ ;
+ munmap(xonly, page_size);
+ return ret;
+}
+
+DEFINE_SUITE("Synthesize MMAP2 events with PROCMAP_QUERY", synthesize_procmap_query);
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index c0fb260d3f2f..7d259c24a917 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -156,6 +156,7 @@ DECLARE_SUITE(dwarf_unwind);
DECLARE_SUITE(expr);
DECLARE_SUITE(hists_filter);
DECLARE_SUITE(mmap_thread_lookup);
+DECLARE_SUITE(synthesize_procmap_query);
DECLARE_SUITE(thread_maps_share);
DECLARE_SUITE(thread_stack);
DECLARE_SUITE(hists_output);
--
2.56.1.915.g69d5ec7298-goog
prev parent reply other threads:[~2026-12-06 2:35 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-06 2:34 [PATCH v5 0/7] perf synthetic-events: Use PROCMAP_QUERY for mmap synthesis Ian Rogers
2026-12-06 2:34 ` [PATCH v5 1/7] perf machine: Fix leak when creating host kernel maps fails Ian Rogers
2026-12-06 2:34 ` [PATCH v5 2/7] perf synthetic-events: Fix race on DSO build IDs when synthesizing Ian Rogers
2026-12-06 2:34 ` [PATCH v5 3/7] perf bench synthesize: Initialize symbols so build IDs can be read Ian Rogers
2026-12-06 2:34 ` [PATCH v5 4/7] tools headers UAPI: Synchronize linux/fs.h with the kernel sources Ian Rogers
2026-12-06 2:34 ` [PATCH v5 5/7] perf synthetic-events: Use PROCMAP_QUERY to synthesize mmap events Ian Rogers
2026-12-06 2:34 ` [PATCH v5 6/7] perf bench synthesize: Add --procmap-query option Ian Rogers
2026-12-06 2:34 ` Ian Rogers [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261206023413.3078292-8-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=andrii@kernel.org \
--cc=dsahern@gmail.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®