Linux Perf Users
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	 Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: Marc Zyngier <maz@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	linux-perf-users@vger.kernel.org,  kvm@vger.kernel.org,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Ian Rogers <irogers@google.com>
Subject: [RFC PATCH 16/16] perf test: Add PERF_SAMPLE_HOST_IP symbolization, sorting, and ASLR tests
Date: Tue,  1 Dec 2026 15:21:16 -0800	[thread overview]
Message-ID: <20261201232116.1988791-17-irogers@google.com> (raw)
In-Reply-To: <20261201232116.1988791-1-irogers@google.com>

Add a dedicated host-ip-sym C test suite covering:

- Automatic guest-to-host symbolization fallback across non-executable
  (PROT_READ) file-backed host MMAP2 mappings with non-zero pgoff,
  default host-only (!perf_guest) mode, perf kvm --guest (!perf_host)
  mode, guest symbol precedence when guest mappings are present, and
  build_id__mark_dso_hit() DSO hit marking via sample->host_ip.
- Histogram collapsing, sorting, and formatting for host_dso, host_sym,
  and host_ip sort keys.
- ASLR address anonymization of sampled host_ip values in aslr_tool.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/tests/Build          |   1 +
 tools/perf/tests/builtin-test.c |   1 +
 tools/perf/tests/host-ip-sym.c  | 691 ++++++++++++++++++++++++++++++++
 tools/perf/tests/tests.h        |   1 +
 4 files changed, 694 insertions(+)
 create mode 100644 tools/perf/tests/host-ip-sym.c

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 58858f3d2bf9..ace316b165a1 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -73,6 +73,7 @@ perf-test-y += hwmon_pmu.o
 perf-test-y += tool_pmu.o
 perf-test-y += subcmd-help.o
 perf-test-y += kallsyms-split.o
+perf-test-y += host-ip-sym.o
 
 ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc riscv))
 perf-test-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 0b8897fd42ca..f7388c5acf2b 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -177,6 +177,7 @@ static struct test_suite *generic_tests[] = {
 	&suite__util,
 	&suite__subcmd_help,
 	&suite__kallsyms_split,
+	&suite__host_ip_sym,
 	NULL,
 };
 
diff --git a/tools/perf/tests/host-ip-sym.c b/tools/perf/tests/host-ip-sym.c
new file mode 100644
index 000000000000..b2acf08446e9
--- /dev/null
+++ b/tools/perf/tests/host-ip-sym.c
@@ -0,0 +1,691 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+
+#include <linux/kernel.h>
+#include <linux/perf_event.h>
+#include <linux/rbtree.h>
+
+#include "perf.h"
+#include "tests/tests.h"
+#include "util/addr_location.h"
+#include "util/aslr.h"
+#include "util/build-id.h"
+#include "util/debug.h"
+#include "util/dso.h"
+#include "util/event.h"
+#include "util/evlist.h"
+#include "util/evsel.h"
+#include "util/hist.h"
+#include "util/machine.h"
+#include "util/map.h"
+#include "util/parse-events.h"
+#include "util/sample.h"
+#include "util/sort.h"
+#include "util/symbol.h"
+#include "util/synthetic-events.h"
+#include "util/thread.h"
+#include "util/tool.h"
+
+#define TEST_HV_PID		4242
+#define TEST_VCPU_TID		4243
+#define TEST_DAX_START		0x7f0000200000ULL
+#define TEST_DAX_LEN		0x10000ULL
+#define TEST_DAX_PGOFF		0x4000ULL
+#define TEST_DAX_SYM1_OFF	0x4100ULL
+#define TEST_DAX_SYM2_OFF	0x4200ULL
+#define TEST_DAX_SYM_LEN	0x80ULL
+#define TEST_HOST_IP1		(TEST_DAX_START + (TEST_DAX_SYM1_OFF - TEST_DAX_PGOFF) + 0x20ULL)
+#define TEST_HOST_IP2		(TEST_DAX_START + (TEST_DAX_SYM2_OFF - TEST_DAX_PGOFF) + 0x20ULL)
+#define TEST_GUEST_IP		0x401120ULL
+
+static void cleanup_machines(struct machines *machines)
+{
+	struct rb_node *nd;
+
+	while ((nd = rb_first_cached(&machines->guests)) != NULL) {
+		struct machine *guest = rb_entry(nd, struct machine, rb_node);
+
+		rb_erase_cached(nd, &machines->guests);
+		machine__delete(guest);
+	}
+	machines__exit(machines);
+}
+
+static void delete_all_hist_entries(struct hists *hists)
+{
+	struct rb_root_cached *root_in;
+	struct rb_root_cached *root_out = &hists->entries;
+	struct rb_node *node;
+
+	if (hists__has(hists, need_collapse))
+		root_in = &hists->entries_collapsed;
+	else
+		root_in = hists->entries_in;
+
+	while (!RB_EMPTY_ROOT(&root_out->rb_root)) {
+		struct hist_entry *he;
+
+		node = rb_first_cached(root_out);
+		he = rb_entry(node, struct hist_entry, rb_node);
+		rb_erase_cached(node, root_out);
+		rb_erase_cached(&he->rb_node_in, root_in);
+		hist_entry__delete(he);
+	}
+}
+
+static int setup_host_dax_mapping(struct machine *host_machine)
+{
+	struct thread *thread;
+	struct dso *dso;
+	struct symbol *sym1, *sym2;
+	struct perf_sample mmap_sample = {
+		.pid = TEST_HV_PID,
+		.tid = TEST_HV_PID,
+		.cpumode = PERF_RECORD_MISC_USER,
+	};
+	union perf_event mmap2_ev = {
+		.mmap2 = {
+			.header = {
+				.type = PERF_RECORD_MMAP2,
+				.misc = PERF_RECORD_MISC_USER | PERF_RECORD_MISC_MMAP_DATA,
+				.size = sizeof(struct perf_record_mmap2),
+			},
+			.pid = TEST_HV_PID,
+			.tid = TEST_HV_PID,
+			.start = TEST_DAX_START,
+			.len = TEST_DAX_LEN,
+			.pgoff = TEST_DAX_PGOFF,
+			.maj = 8,
+			.min = 1,
+			.ino = 123456,
+			.ino_generation = 1,
+			.prot = PROT_READ,
+			.flags = MAP_SHARED,
+		},
+	};
+
+	snprintf(mmap2_ev.mmap2.filename, sizeof(mmap2_ev.mmap2.filename),
+		 "/mnt/dax/guest_app");
+
+	thread = machine__findnew_thread(host_machine, TEST_HV_PID, TEST_VCPU_TID);
+	if (!thread)
+		return -ENOMEM;
+	thread__set_comm(thread, "qemu-vcpu", 0);
+	thread__put(thread);
+
+	if (machine__process_mmap2_event(host_machine, &mmap2_ev, &mmap_sample) < 0)
+		return -1;
+
+	dso = machine__findnew_dso(host_machine, "/mnt/dax/guest_app");
+	if (!dso)
+		return -ENOMEM;
+
+	dso__set_loaded(dso);
+
+	sym1 = symbol__new(TEST_DAX_SYM1_OFF, TEST_DAX_SYM_LEN,
+			   STB_GLOBAL, STT_FUNC, "dax_guest_hot_fn");
+	sym2 = symbol__new(TEST_DAX_SYM2_OFF, TEST_DAX_SYM_LEN,
+			   STB_GLOBAL, STT_FUNC, "dax_guest_cold_fn");
+	if (!sym1 || !sym2) {
+		if (sym1)
+			symbol__delete(sym1);
+		if (sym2)
+			symbol__delete(sym2);
+		dso__put(dso);
+		return -ENOMEM;
+	}
+
+	symbols__insert(dso__symbols(dso), sym1);
+	symbols__insert(dso__symbols(dso), sym2);
+	dso__put(dso);
+	return 0;
+}
+
+static int test__host_ip_fallback(struct test_suite *test __maybe_unused,
+				  int subtest __maybe_unused)
+{
+	struct machines machines = { 0 };
+	struct machine *guest_machine;
+	struct addr_location al;
+	struct dso *guest_dso;
+	struct symbol *guest_sym;
+	bool saved_host = perf_host;
+	bool saved_guest = perf_guest;
+	int ret = TEST_FAIL;
+	struct perf_sample sample = {
+		.pid = TEST_HV_PID,
+		.tid = TEST_VCPU_TID,
+		.ip = TEST_GUEST_IP,
+		.host_ip = TEST_HOST_IP1,
+		.period = 100,
+		.cpumode = PERF_RECORD_MISC_GUEST_USER,
+	};
+	struct perf_sample guest_mmap_sample = {
+		.pid = TEST_HV_PID,
+		.tid = TEST_VCPU_TID,
+		.cpumode = PERF_RECORD_MISC_GUEST_USER,
+	};
+	union perf_event guest_mmap_ev = {
+		.mmap = {
+			.header = {
+				.type = PERF_RECORD_MMAP,
+				.misc = PERF_RECORD_MISC_GUEST_USER,
+				.size = sizeof(struct perf_record_mmap),
+			},
+			.pid = TEST_HV_PID,
+			.tid = TEST_VCPU_TID,
+			.start = 0x401000ULL,
+			.len = 0x1000ULL,
+			.pgoff = 0,
+		},
+	};
+
+	if (machines__init(&machines, /*session=*/NULL))
+		return TEST_FAIL;
+
+	perf_host = true;
+	perf_guest = true;
+
+	if (setup_host_dax_mapping(&machines.host))
+		goto out_machines;
+
+	guest_machine = machines__find_guest(&machines, TEST_HV_PID);
+	if (!guest_machine)
+		goto out_machines;
+
+	addr_location__init(&al);
+
+	/*
+	 * 1. Guest sample with no guest symbols automatically falls back to
+	 *    the non-executable DAX mapping on machines.host.
+	 */
+	if (machine__resolve(guest_machine, &al, &sample) < 0)
+		goto out_al;
+
+	if (!al.map || !al.sym || strcmp(al.sym->name, "dax_guest_hot_fn") != 0) {
+		pr_debug("Expected fallback sym dax_guest_hot_fn, got %s\n",
+			 al.sym ? al.sym->name : "<null>");
+		goto out_al;
+	}
+	if (al.addr != TEST_DAX_SYM1_OFF + 0x20ULL || al.level != 'u') {
+		pr_debug("Unexpected fallback addr=%#" PRIx64 " level=%c\n",
+			 al.addr, al.level);
+		goto out_al;
+	}
+	if (!al.host_map || !al.host_sym ||
+	    strcmp(al.host_sym->name, "dax_guest_hot_fn") != 0 ||
+	    al.host_addr != TEST_DAX_SYM1_OFF + 0x20ULL ||
+	    al.raw_host_ip != TEST_HOST_IP1) {
+		pr_debug("Explicit host_map/host_sym mismatch\n");
+		goto out_al;
+	}
+	addr_location__exit(&al);
+
+	/*
+	 * 2. In 'perf kvm --guest' mode (perf_host == false, perf_guest == true),
+	 *    host_ip lookup on machines.host must still succeed without setting
+	 *    HIST_FILTER__HOST.
+	 */
+	perf_host = false;
+	perf_guest = true;
+	addr_location__init(&al);
+	if (machine__resolve(guest_machine, &al, &sample) < 0)
+		goto out_al;
+
+	if (al.filtered != 0 || !al.sym ||
+	    strcmp(al.sym->name, "dax_guest_hot_fn") != 0) {
+		pr_debug("perf kvm --guest mode failed: filtered=%#x sym=%s\n",
+			 al.filtered, al.sym ? al.sym->name : "<null>");
+		goto out_al;
+	}
+	addr_location__exit(&al);
+	perf_host = true;
+
+	/*
+	 * 2b. In default perf report/script mode (perf_host == true,
+	 *     perf_guest == false), resolving a guest sample via machines.host
+	 *     must clear HIST_FILTER__GUEST when falling back to host_map.
+	 */
+	perf_host = true;
+	perf_guest = false;
+	addr_location__init(&al);
+	if (machine__resolve(&machines.host, &al, &sample) < 0)
+		goto out_al;
+
+	if (al.filtered != 0 || al.level != 'u' || !al.sym ||
+	    strcmp(al.sym->name, "dax_guest_hot_fn") != 0) {
+		pr_debug("default host-only mode failed: filtered=%#x level=%c sym=%s\n",
+			 al.filtered, al.level, al.sym ? al.sym->name : "<null>");
+		goto out_al;
+	}
+	addr_location__exit(&al);
+	perf_guest = true;
+
+	/*
+	 * 2c. build_id__mark_dso_hit() must mark the host DSO hit via
+	 *     sample.host_ip for guest samples so perf record --host-ip
+	 *     caches its build-id.
+	 */
+	{
+		struct perf_event_attr attr = {
+			.type = PERF_TYPE_HARDWARE,
+			.config = PERF_COUNT_HW_CPU_CYCLES,
+			.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID |
+				       PERF_SAMPLE_HOST_IP,
+		};
+		union perf_event sample_ev = {
+			.header = {
+				.type = PERF_RECORD_SAMPLE,
+				.misc = PERF_RECORD_MISC_GUEST_USER,
+				.size = sizeof(struct perf_event_header),
+			},
+		};
+		struct evsel *evsel = evsel__new(&attr);
+		struct dso *host_dso = machine__findnew_dso(&machines.host,
+							    "/mnt/dax/guest_app");
+		int hit_err;
+
+		if (!evsel || !host_dso) {
+			evsel__put(evsel);
+			dso__put(host_dso);
+			goto out_machines;
+		}
+		if (dso__hit(host_dso)) {
+			pr_debug("host DSO unexpectedly marked hit before build_id__mark_dso_hit\n");
+			evsel__put(evsel);
+			dso__put(host_dso);
+			goto out_machines;
+		}
+		sample.evsel = evsel;
+		hit_err = build_id__mark_dso_hit(/*tool=*/NULL, &sample_ev,
+						 &sample, guest_machine);
+		sample.evsel = NULL;
+		if (hit_err < 0 || !dso__hit(host_dso)) {
+			pr_debug("build_id__mark_dso_hit failed to mark host DSO hit\n");
+			evsel__put(evsel);
+			dso__put(host_dso);
+			goto out_machines;
+		}
+		evsel__put(evsel);
+		dso__put(host_dso);
+	}
+
+	/*
+	 * 3. When the guest machine DOES have a symbol for sample.ip, guest
+	 *    symbolization takes precedence for al.sym while al.host_sym still
+	 *    exposes the host symbol.
+	 */
+	snprintf(guest_mmap_ev.mmap.filename, sizeof(guest_mmap_ev.mmap.filename),
+		 "/bin/guest_native");
+	if (machine__process_mmap_event(guest_machine, &guest_mmap_ev,
+					&guest_mmap_sample) < 0)
+		goto out_machines;
+
+	guest_dso = machine__findnew_dso(guest_machine, "/bin/guest_native");
+	if (!guest_dso)
+		goto out_machines;
+	dso__set_loaded(guest_dso);
+	guest_sym = symbol__new(0x100ULL, 0x80ULL, STB_GLOBAL, STT_FUNC,
+				"guest_native_fn");
+	if (!guest_sym) {
+		dso__put(guest_dso);
+		goto out_machines;
+	}
+	symbols__insert(dso__symbols(guest_dso), guest_sym);
+	dso__put(guest_dso);
+
+	addr_location__init(&al);
+	if (machine__resolve(guest_machine, &al, &sample) < 0)
+		goto out_al;
+
+	if (!al.sym || strcmp(al.sym->name, "guest_native_fn") != 0 ||
+	    !al.host_sym || strcmp(al.host_sym->name, "dax_guest_hot_fn") != 0) {
+		pr_debug("Guest symbol precedence failed: sym=%s host_sym=%s\n",
+			 al.sym ? al.sym->name : "<null>",
+			 al.host_sym ? al.host_sym->name : "<null>");
+		goto out_al;
+	}
+
+	ret = TEST_OK;
+out_al:
+	addr_location__exit(&al);
+out_machines:
+	perf_host = saved_host;
+	perf_guest = saved_guest;
+	cleanup_machines(&machines);
+	return ret;
+}
+
+static int add_guest_hist_sample(struct evsel *evsel, struct machine *guest_machine,
+				 u64 guest_ip, u64 host_ip)
+{
+	struct addr_location al;
+	struct perf_sample sample = {
+		.evsel = evsel,
+		.pid = TEST_HV_PID,
+		.tid = TEST_VCPU_TID,
+		.cpu = 0,
+		.ip = guest_ip,
+		.host_ip = host_ip,
+		.period = 100,
+		.cpumode = PERF_RECORD_MISC_GUEST_USER,
+	};
+	struct hist_entry_iter iter = {
+		.sample = &sample,
+		.ops = &hist_iter_normal,
+		.hide_unresolved = false,
+	};
+	int err;
+
+	addr_location__init(&al);
+	err = machine__resolve(guest_machine, &al, &sample);
+	if (!err)
+		err = hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack, NULL);
+	addr_location__exit(&al);
+	return err;
+}
+
+static int test__host_ip_hist_sort(struct test_suite *test __maybe_unused,
+				   int subtest __maybe_unused)
+{
+	struct machines machines = { 0 };
+	struct machine *guest_machine;
+	struct evlist *evlist;
+	struct evsel *evsel;
+	struct hists *hists;
+	struct rb_node *node;
+	struct hist_entry *he;
+	char buf[512];
+	struct perf_hpp hpp = {
+		.buf = buf,
+		.size = sizeof(buf),
+	};
+	bool saved_host = perf_host;
+	bool saved_guest = perf_guest;
+	int ret = TEST_FAIL;
+
+	evlist = evlist__new();
+	if (!evlist)
+		return TEST_FAIL;
+
+	if (parse_event(evlist, "cpu-clock"))
+		goto out_evlist;
+
+	if (machines__init(&machines, /*session=*/NULL))
+		goto out_evlist;
+
+	perf_host = true;
+	perf_guest = true;
+
+	if (setup_host_dax_mapping(&machines.host))
+		goto out_machines;
+
+	guest_machine = machines__find_guest(&machines, TEST_HV_PID);
+	if (!guest_machine)
+		goto out_machines;
+
+	field_order = NULL;
+	sort_order = "host_dso,host_sym,host_ip";
+	if (setup_sorting(/*evlist=*/NULL, machines.host.env) < 0)
+		goto out_machines;
+	perf_hpp__init();
+
+	evsel = evlist__first(evlist);
+	hists = evsel__hists(evsel);
+
+	if (add_guest_hist_sample(evsel, guest_machine, TEST_GUEST_IP, TEST_HOST_IP1) ||
+	    add_guest_hist_sample(evsel, guest_machine, TEST_GUEST_IP + 4, TEST_HOST_IP1) ||
+	    add_guest_hist_sample(evsel, guest_machine, TEST_GUEST_IP + 0x100, TEST_HOST_IP2))
+		goto out_hists;
+
+	hists__collapse_resort(hists, NULL);
+	evsel__output_resort(evsel, NULL);
+
+	node = rb_first_cached(&hists->entries);
+	if (!node)
+		goto out_hists;
+
+	he = rb_entry(node, struct hist_entry, rb_node);
+	if (he->stat.period != 200 || !he->host_ms.sym ||
+	    strcmp(he->host_ms.sym->name, "dax_guest_hot_fn") != 0 ||
+	    !he->host_ms.map ||
+	    strcmp(dso__short_name(map__dso(he->host_ms.map)), "guest_app") != 0 ||
+	    map__unmap_ip(he->host_ms.map, he->host_ip) != TEST_HOST_IP1) {
+		pr_debug("Unexpected top hist_entry for host sort keys\n");
+		goto out_hists;
+	}
+
+	hists__calc_col_len(hists, he);
+	{
+		struct perf_hpp_fmt *fmt;
+
+		buf[0] = '\0';
+		hists__for_each_format(hists, fmt) {
+			int printed = fmt->entry(fmt, &hpp, he);
+
+			if (printed > 0)
+				advance_hpp(&hpp, printed);
+		}
+	}
+	if (!strstr(buf, "guest_app") || !strstr(buf, "dax_guest_hot_fn") ||
+	    !strstr(buf, "7f0000200120")) {
+		pr_debug("Formatted hist_entry missing expected host fields: '%s'\n", buf);
+		goto out_hists;
+	}
+
+	node = rb_next(node);
+	if (!node)
+		goto out_hists;
+	he = rb_entry(node, struct hist_entry, rb_node);
+	if (he->stat.period != 100 || !he->host_ms.sym ||
+	    strcmp(he->host_ms.sym->name, "dax_guest_cold_fn") != 0) {
+		pr_debug("Unexpected second hist_entry for host sort keys\n");
+		goto out_hists;
+	}
+
+	/*
+	 * Verify collapsing a sample with host_ip == 0 into an existing
+	 * comm-sorted entry preserves valid host_ms.map/sym references.
+	 */
+	delete_all_hist_entries(hists);
+	reset_output_field();
+	sort_order = "comm";
+	if (setup_sorting(/*evlist=*/NULL, machines.host.env) < 0)
+		goto out_machines;
+	perf_hpp__init();
+
+	if (add_guest_hist_sample(evsel, guest_machine, TEST_GUEST_IP, TEST_HOST_IP1) ||
+	    add_guest_hist_sample(evsel, guest_machine, TEST_GUEST_IP + 4, 0))
+		goto out_hists;
+
+	hists__collapse_resort(hists, NULL);
+	evsel__output_resort(evsel, NULL);
+	node = rb_first_cached(&hists->entries);
+	if (!node)
+		goto out_hists;
+	he = rb_entry(node, struct hist_entry, rb_node);
+	if (he->stat.period != 200 || !he->host_ms.map || !he->host_ms.sym ||
+	    strcmp(he->host_ms.sym->name, "dax_guest_hot_fn") != 0) {
+		pr_debug("Collapsing sample with host_ip=0 corrupted host_ms\n");
+		goto out_hists;
+	}
+
+	ret = TEST_OK;
+out_hists:
+	delete_all_hist_entries(hists);
+	reset_output_field();
+	sort_order = NULL;
+out_machines:
+	perf_host = saved_host;
+	perf_guest = saved_guest;
+	cleanup_machines(&machines);
+out_evlist:
+	evlist__put(evlist);
+	return ret;
+}
+
+struct aslr_capture_tool {
+	struct perf_tool tool;
+	u64 remapped_host_ip[2];
+	int count;
+};
+
+static int capture_sample_cb(const struct perf_tool *tool,
+			     union perf_event *event __maybe_unused,
+			     struct perf_sample *sample,
+			     struct machine *machine __maybe_unused)
+{
+	struct aslr_capture_tool *cap =
+		container_of(tool, struct aslr_capture_tool, tool);
+
+	if (cap->count < 2)
+		cap->remapped_host_ip[cap->count] = sample->host_ip;
+	cap->count++;
+	return 0;
+}
+
+static int nop_mmap2_cb(const struct perf_tool *tool __maybe_unused,
+			union perf_event *event __maybe_unused,
+			struct perf_sample *sample __maybe_unused,
+			struct machine *machine __maybe_unused)
+{
+	return 0;
+}
+
+static int test__host_ip_aslr(struct test_suite *test __maybe_unused,
+			      int subtest __maybe_unused)
+{
+	struct aslr_capture_tool cap = { .count = 0 };
+	struct perf_tool *aslr;
+	struct machines machines = { 0 };
+	struct machine *guest_machine;
+	struct perf_event_attr attr = {
+		.type = PERF_TYPE_HARDWARE,
+		.config = PERF_COUNT_HW_CPU_CYCLES,
+		.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_HOST_IP,
+	};
+	struct evsel *evsel = NULL;
+	union perf_event *ev = NULL;
+	union perf_event mmap2_ev = {
+		.mmap2 = {
+			.header = {
+				.type = PERF_RECORD_MMAP2,
+				.misc = PERF_RECORD_MISC_USER | PERF_RECORD_MISC_MMAP_DATA,
+				.size = sizeof(struct perf_record_mmap2),
+			},
+			.pid = TEST_HV_PID,
+			.tid = TEST_HV_PID,
+			.start = TEST_DAX_START,
+			.len = TEST_DAX_LEN,
+			.pgoff = TEST_DAX_PGOFF,
+			.prot = PROT_READ,
+			.flags = MAP_SHARED,
+		},
+	};
+	struct perf_sample mmap_sample = {
+		.pid = TEST_HV_PID,
+		.tid = TEST_HV_PID,
+		.cpumode = PERF_RECORD_MISC_USER,
+	};
+	struct perf_sample s1 = {
+		.ip = TEST_GUEST_IP,
+		.pid = TEST_HV_PID,
+		.tid = TEST_VCPU_TID,
+		.host_ip = TEST_HOST_IP1,
+		.cpumode = PERF_RECORD_MISC_GUEST_USER,
+	};
+	struct perf_sample s2 = {
+		.ip = TEST_GUEST_IP,
+		.pid = TEST_HV_PID,
+		.tid = TEST_VCPU_TID,
+		.host_ip = 0,
+		.cpumode = PERF_RECORD_MISC_GUEST_USER,
+	};
+	size_t sz;
+	int ret = TEST_FAIL;
+
+	snprintf(mmap2_ev.mmap2.filename, sizeof(mmap2_ev.mmap2.filename),
+		 "/mnt/dax/guest_app");
+
+	if (machines__init(&machines, /*session=*/NULL))
+		return TEST_FAIL;
+
+	guest_machine = machines__find_guest(&machines, TEST_HV_PID);
+	if (!guest_machine)
+		goto out_machines;
+
+	perf_tool__init(&cap.tool, /*ordered_events=*/false);
+	cap.tool.sample = capture_sample_cb;
+	cap.tool.mmap2 = nop_mmap2_cb;
+
+	aslr = aslr_tool__new(&cap.tool);
+	if (!aslr)
+		goto out_machines;
+
+	evsel = evsel__new(&attr);
+	if (!evsel)
+		goto out_aslr;
+	evsel->sample_size = __evsel__sample_size(attr.sample_type);
+
+	if (aslr->mmap2(aslr, &mmap2_ev, &mmap_sample, &machines.host) < 0)
+		goto out_evsel;
+
+	sz = perf_event__sample_event_size(&s1, attr.sample_type, 0, 0);
+	ev = calloc(1, sz);
+	if (!ev)
+		goto out_evsel;
+
+	ev->header.type = PERF_RECORD_SAMPLE;
+	ev->header.misc = PERF_RECORD_MISC_GUEST_USER;
+	ev->header.size = sz;
+
+	s1.evsel = evsel;
+	if (perf_event__synthesize_sample(ev, attr.sample_type, 0, 0, &s1))
+		goto out_ev;
+	if (aslr->sample(aslr, ev, &s1, guest_machine) < 0)
+		goto out_ev;
+
+	s2.evsel = evsel;
+	if (perf_event__synthesize_sample(ev, attr.sample_type, 0, 0, &s2))
+		goto out_ev;
+	if (aslr->sample(aslr, ev, &s2, guest_machine) < 0)
+		goto out_ev;
+
+	if (cap.count != 2 || cap.remapped_host_ip[0] == 0 ||
+	    cap.remapped_host_ip[0] == TEST_HOST_IP1 ||
+	    (cap.remapped_host_ip[0] & 0xfffULL) != (TEST_HOST_IP1 & 0xfffULL) ||
+	    cap.remapped_host_ip[1] != 0) {
+		pr_debug("Unexpected ASLR host_ip remapping: %#" PRIx64 ", %#" PRIx64 "\n",
+			 cap.remapped_host_ip[0], cap.remapped_host_ip[1]);
+		goto out_ev;
+	}
+
+	ret = TEST_OK;
+out_ev:
+	free(ev);
+out_evsel:
+	evsel__put(evsel);
+out_aslr:
+	aslr_tool__delete(aslr);
+out_machines:
+	cleanup_machines(&machines);
+	return ret;
+}
+
+static struct test_case tests__host_ip_sym[] = {
+	TEST_CASE("Guest fallback symbolization via host IP", host_ip_fallback),
+	TEST_CASE("Histogram sorting and formatting for host IP keys", host_ip_hist_sort),
+	TEST_CASE("ASLR anonymization of sampled host IP", host_ip_aslr),
+	{ .name = NULL, }
+};
+
+struct test_suite suite__host_ip_sym = {
+	.desc = "PERF_SAMPLE_HOST_IP symbolization and sorting",
+	.test_cases = tests__host_ip_sym,
+};
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 20ae3609892b..3dce04124d6c 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -201,6 +201,7 @@ DECLARE_SUITE(util);
 DECLARE_SUITE(uncore_event_sorting);
 DECLARE_SUITE(subcmd_help);
 DECLARE_SUITE(kallsyms_split);
+DECLARE_SUITE(host_ip_sym);
 
 /*
  * PowerPC and S390 do not support creation of instruction breakpoints using the
-- 
2.56.1.915.g69d5ec7298-goog


  parent reply	other threads:[~2026-12-01 23:22 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 23:21 [RFC PATCH 00/16] perf, kvm: Add PERF_SAMPLE_HOST_IP for host-side guest symbolization Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 01/16] perf/core: Add PERF_SAMPLE_HOST_IP sample format Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 02/16] perf/core, bpf: Support BPF filtering of mmap_data events Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 03/16] KVM: Add NMI-safe guest-to-host IP translation infrastructure Ian Rogers
2026-12-01 23:40   ` sashiko-bot
2026-12-01 23:21 ` [RFC PATCH 04/16] KVM: x86: Implement kvm_arch_vcpu_get_ip_gpa() Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 05/16] KVM: arm64: " Ian Rogers
2026-12-01 23:41   ` sashiko-bot
2026-12-01 23:21 ` [RFC PATCH 06/16] KVM: riscv: " Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 07/16] KVM: loongarch: " Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 08/16] tools headers UAPI: Sync linux/perf_event.h with the kernel sources Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 09/16] perf tools: Add PERF_SAMPLE_HOST_IP parsing, synthesis, and record --host-ip Ian Rogers
2026-12-01 23:36   ` sashiko-bot
2026-12-01 23:21 ` [RFC PATCH 10/16] perf symbol: Support host IP resolution and guest fallback symbolization Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 11/16] perf report, script: Add host_ip, host_dso, and host_sym fields Ian Rogers
2026-12-01 23:41   ` sashiko-bot
2026-12-01 23:21 ` [RFC PATCH 12/16] perf record: Synthesize hypervisor file-backed data mappings for --host-ip Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 13/16] perf record: Filter runtime hypervisor data mmaps with BPF " Ian Rogers
2026-12-01 23:43   ` sashiko-bot
2026-12-01 23:21 ` [RFC PATCH 14/16] perf bpf-filter: Support host_ip in BPF sample filters Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 15/16] perf aslr: Anonymize sampled host_ip values Ian Rogers
2026-12-01 23:21 ` Ian Rogers [this message]
2026-12-01 23:40   ` [RFC PATCH 16/16] perf test: Add PERF_SAMPLE_HOST_IP symbolization, sorting, and ASLR tests sashiko-bot
2026-12-01 23:43 ` [RFC PATCH 00/16] perf, kvm: Add PERF_SAMPLE_HOST_IP for host-side guest symbolization sashiko-bot

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=20261201232116.1988791-17-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.com \
    /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