From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796365427; b=krnKsRuV9f6tbpbvO2AyC2tocNZd2B3U5qeQHZcKjQ9XZYpwqC5rZVS1xD0pBl5tIIDf UURlxxeIPgPNsJ19NeXmf4VFYwtZ4bwMKJ9kYAbKZH3STzBUqur/kxu/YEadteK43DjeP 3c4oNaUWl13sRExZE0w2t4bYpvKj3e1QQ7IqQcJ3W+Bis0b0dH4ZymWvBXaGwuJPxk5eZ RHMESk5AMUxPG/Ny4r28B2c7TOgjttnT9vd5rvVBZs3aB3Fl5svgjai5BxgN40T7uE71G fN4dxcQ0pKcCR2tfAmxsKE20pRYw6GJpMdAuH7p8e2MTyJRUSJWEaC5TlzMWxGW8JJw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796365427; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Received:Date:In-Reply-To:Mime-Version: References:X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=OvO3wNT8w0S2B58hqOAT6YoXfIlpKllo5MZQKFi/+HA=; b=Fnyc+aeiKe2xQmuyE2Dk3fnMP6OTNYv33qWujGrT2X00En8uicik0JBd9UasGdDr3rPS CDlZRpK4Vtx/flM3vrzEzQZrXcrdV+cF2NlxWPBBNeszf7ozRNp5iXr5GRN+t4Ta1I/oI acAbLUcR9b1ajHaHAbYIzYBKNvgIvAiZNxVDXv/0nToSQzlH/gkJGRTLVRDMSx7n5bPzZ m/a42Y1IfvB+cf0fjTnUhZ5RnMjEh0X7PR+Vd6RiACMi+AEhAReEUbFI79rZB/3dPJJbB NupPaCXnmP5rhxhsFSmXyVmX+mBEQ/seN7u7qP4ODjAl+bc1SmbRPyKBg5vRxvsDDPA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=gabaWriZ; arc=none smtp.remote-ip=209.85.210.70 Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC42D606A33E for ; Fri, 4 Dec 2026 06:23:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EC42D606A33E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=gabaWriZ Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-862ad1e3959so459984a34.2 for ; Thu, 03 Dec 2026 22:23:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796365426; x=1796970226; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=OvO3wNT8w0S2B58hqOAT6YoXfIlpKllo5MZQKFi/+HA=; b=gabaWriZPCFTQNjjCZ+8/tOJu86KzJukeP3tQQdY7xdPkamCPYNaaJxri9SxOk5Dzk zy/xDlUzFwzFvIZvTWtUjBgZWHa0Fb/NnX9a3c9QVV5/ntseIA/Mbx9GEPmH02pY+Fil GSDhbym6RDGVcjHS8LhBiHEJpB91ocwMy4DPb1kyIfumR2t4IFARiP68ojXi9WCMzf1F crq2Fa2rQBm0fDWGcAq8jsfMthxZGJnXKL7QGhNkJB3TcruM2fqDy8IoryZ6NDhPcp+4 LFBM8QYLuN5Lx2c2qEctCm/mxOrmEPLBgE6o0ovVSlo+1gVLX8PXjWuSDo2fCyPaMKNJ e1mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796365426; x=1796970226; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=OvO3wNT8w0S2B58hqOAT6YoXfIlpKllo5MZQKFi/+HA=; b=cOF+i4Hbrv0t7wGbBiHiaO/MBj3eQeaQJyVrAJUX9jpmA7GbWWsOKk9R73rYl1pAuz Bh5VZeeUEPadHe8eCxG+DEfsJ++BWKE/49TeW8x/l/s9IZCXwwdsCuBKRH6gAumZOe/P bQeidQmutXEALUqaidjvSV+hRuIBAZhpdxTQ9bT5ies8l5CN4y2n4WbwuxQDsojeHkNq OsgpaD6hWi/O3FhYBp0sNQHAEopZMggnXlzt3RG0TCPIBnR8K2t6mI9KVkXBjPh0/sYL a6IIF0VTS9+wTtqr4Sw0t47zcROmoYFtxfhM5pNKiW68+F54yhRy41Yho/Nv7oRXeU6J giEQ== X-Gm-Message-State: APuU4Z+ofNyrYhEUSnaj15t/mnNH+FncI87pW0VY/8lp/gmqErhpd3mb KJt+8YAuIofZxb56gR2iWQWQJrRWAD4pdLIINUHpSM4QctqTIE6CDEQW3gFjtlfEcaqNaujzVWg zxR4PNmRW6Q== X-Received: from oobby26.prod.google.com ([2002:a05:6820:201a:b0:71a:ef5f:73d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:617:b0:6d7:a13d:fd7b with SMTP id 006d021491bc7-71bf2073678mr778309eaf.28.1796365425709; Thu, 03 Dec 2026 22:23:45 -0800 (PST) Date: Thu, 3 Dec 2026 22:23:28 -0800 In-Reply-To: <20261204062328.2508164-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261204062328.2508164-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261204062328.2508164-8-irogers@google.com> Subject: [PATCH v3 7/7] perf test: Add test comparing PROCMAP_QUERY and /proc/pid/maps synthesis From: Ian Rogers To: linux-perf-users@vger.kernel.org Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, jolsa@kernel.org, adrian.hunter@intel.com, james.clark@linaro.org, dsahern@gmail.com, andrii@kernel.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" Synthesize the MMAP2 events of the perf test process both by parsing /proc//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 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 so that the DSO build ID cache isn't shared. 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. With data mmaps only file backed mappings are compared, not "//anon" ones, as anonymous mappings like the heap may change between the two synthesis runs. The gate VMA, such as [vsyscall], is only in the maps file and so it is ignored. To cover the execute-only case, a page of the perf binary is mapped with only PROT_EXEC and the test checks both paths have an event for it. The test is skipped if the kernel doesn't support PROCMAP_QUERY. $ 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 300 MMAP2 events (mmap_data=1, build IDs=1) Compared 62 MMAP2 events (mmap_data=0, build IDs=0) Compared 300 MMAP2 events (mmap_data=1, build IDs=0) 30: Synthesize MMAP2 events with PROCMAP_QUERY : Ok Signed-off-by: Ian Rogers --- Notes: This change was developed with the help of an AI coding assistant (Gemini in Jetski), which wrote the initial code, tests and changelog; it was reviewed, built and tested by the author. tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/synthesize-mmap.c | 275 +++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 4 files changed, 278 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..53c6f2b0eeb4 --- /dev/null +++ b/tools/perf/tests/synthesize-mmap.c @@ -0,0 +1,275 @@ +// 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//maps. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "debug.h" +#include "env.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 the process' 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, bool procmap_query, bool mmap_data, + int fill) +{ + bool saved_procmap_query = synthesize_procmap_query; + struct perf_env host_env; + 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. */ + perf_env__init(&host_env); + machine = machine__new_host(&host_env); + if (!machine) + goto out_env; + 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, getpid(), getpid(), + process_event, machine, mmap_data); + synthesize_procmap_query = saved_procmap_query; + + free(event); +out_machine: + machine__delete(machine); +out_env: + perf_env__exit(&host_env); + 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()); +} + +/* + * When synthesizing data mmaps, anonymous memory like the heap may change + * between the two synthesis runs due to allocations. Only compare file backed + * mappings that should be stable, not "//anon" ones. The gate VMA, such as + * [vsyscall] on x86-64, isn't in the process' VMA tree, it is shown in + * /proc//maps but not reported by PROCMAP_QUERY, so it is ignored. + */ +static bool is_stable(union perf_event *event, bool mmap_data) +{ + const char *name = event->mmap2.filename; + + if (!strcmp(name, "[vsyscall]") || !strcmp(name, "[vectors]")) + return false; + + return !mmap_data || (name[0] == '/' && name[1] != '/'); +} + +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_stable(text->events[i], mmap_data)) + i++; + while (j < query->nr && !is_stable(query->events[j], mmap_data)) + 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; +} + +static bool procmap_query_supported(void) +{ + struct procmap_query q = { + .size = sizeof(q), + .query_flags = PROCMAP_QUERY_COVERING_OR_NEXT_VMA, + }; + int fd = open("/proc/self/maps", O_RDONLY); + bool ret; + + if (fd < 0) + return false; + ret = ioctl(fd, PROCMAP_QUERY, &q) == 0; + close(fd); + return ret; +} + +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; + long page_size = sysconf(_SC_PAGESIZE); + int ret = TEST_OK; + void *xonly; + int fd; + + if (!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; + } + + 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, /*procmap_query=*/false, mmap_data, /*fill=*/0xaa) || + synthesize(&query, /*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); + } + symbol_conf.no_buildid_mmap2 = saved_no_buildid_mmap2; + 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