All of lore.kernel.org
 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 09/16] perf tools: Add PERF_SAMPLE_HOST_IP parsing, synthesis, and record --host-ip
Date: Tue,  1 Dec 2026 15:21:09 -0800	[thread overview]
Message-ID: <20261201232116.1988791-10-irogers@google.com> (raw)
In-Reply-To: <20261201232116.1988791-1-irogers@google.com>

Add core userspace plumbing for PERF_SAMPLE_HOST_IP:

- Add host_ip to struct perf_sample and wire __evsel__parse_sample(),
  perf_event__sample_event_size(), perf_event__synthesize_sample(),
  dump_sample(), and perf_event_attr__fprintf() (placing
  PERF_SAMPLE_HOST_IP right after PERF_SAMPLE_CODE_PAGE_SIZE and before
  PERF_SAMPLE_AUX).
- Add --host-ip to perf record (and perf kvm record) to request
  PERF_SAMPLE_HOST_IP on non-dummy events, probe kernel support in
  evsel__detect_missing_features(), and report a helpful message in
  evsel__open_strerror().
- Mark host DSOs hit via sample->host_ip in build_id__mark_dso_hit() so
  perf record --host-ip caches their build-ids.
- Update the sample-parsing test to cover PERF_SAMPLE_HOST_IP
  (standalone, combined with all sample bits, and truncated bounds
  checking).

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/Documentation/perf-record.txt  |  7 ++++++
 tools/perf/builtin-record.c               |  2 ++
 tools/perf/tests/sample-parsing.c         |  7 +++++-
 tools/perf/util/build-id.c                | 27 +++++++++++++++++++++++
 tools/perf/util/evsel.c                   | 22 ++++++++++++++++++
 tools/perf/util/evsel.h                   |  1 +
 tools/perf/util/perf_event_attr_fprintf.c |  3 ++-
 tools/perf/util/record.h                  |  1 +
 tools/perf/util/sample.h                  |  2 ++
 tools/perf/util/session.c                 |  3 +++
 tools/perf/util/synthetic-events.c        |  8 +++++++
 11 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 2351f7b4f5fc..fb3029f82a08 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -534,6 +534,13 @@ sets are allowed like -I.
 --running-time::
 Record running and enabled time for read events (:S)
 
+--host-ip::
+Record host virtual IP addresses alongside sampled instruction addresses
+(PERF_SAMPLE_HOST_IP). When profiling virtual machines with direct-access (DAX)
+or file-backed hypervisor mappings, this allows host mappings to symbolize guest
+samples without requiring guest symbol information, and filters non-executable
+hypervisor file mappings via BPF when supported.
+
 -k::
 --clockid::
 Sets the clock id to use for the various time fields in the perf_event_type
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 085d3648a30a..4ff929191211 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -3546,6 +3546,8 @@ static struct option __record_options[] = {
 		    "Record the sampled data address data page size"),
 	OPT_BOOLEAN(0, "code-page-size", &record.opts.sample_code_page_size,
 		    "Record the sampled code address (ip) page size"),
+	OPT_BOOLEAN(0, "host-ip", &record.opts.sample_host_ip,
+		    "Record host virtual IP addresses for sampled instructions"),
 	OPT_BOOLEAN(0, "sample-mem-info", &record.opts.sample_data_src,
 		    "Record the data source for memory operations"),
 	OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"),
diff --git a/tools/perf/tests/sample-parsing.c b/tools/perf/tests/sample-parsing.c
index acbd353d3588..9c04eaba813d 100644
--- a/tools/perf/tests/sample-parsing.c
+++ b/tools/perf/tests/sample-parsing.c
@@ -209,6 +209,9 @@ static bool samples_same(struct perf_sample *s1,
 	if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
 		COMP(code_page_size);
 
+	if (type & PERF_SAMPLE_HOST_IP)
+		COMP(host_ip);
+
 	if (type & PERF_SAMPLE_AUX) {
 		COMP(aux_sample.size);
 		if (memcmp(s1->aux_sample.data, s2->aux_sample.data,
@@ -295,6 +298,7 @@ static int do_test(u64 sample_type, u64 sample_regs, u64 read_format)
 			.size	= sizeof(aux_data),
 			.data	= (void *)aux_data,
 		},
+		.host_ip	= 119,
 	};
 	struct sample_read_value values[] = {{1, 5, 0}, {9, 3, 0}, {2, 7, 0}, {6, 4, 1},};
 	struct sample_read_value packed_values[ARRAY_SIZE(values)];
@@ -554,6 +558,7 @@ static int test_truncated_u64_fields(void)
 		PERF_SAMPLE_CGROUP,
 		PERF_SAMPLE_DATA_PAGE_SIZE,
 		PERF_SAMPLE_CODE_PAGE_SIZE,
+		PERF_SAMPLE_HOST_IP,
 	};
 
 	for (size_t i = 0; i < ARRAY_SIZE(types); i++) {
@@ -606,7 +611,7 @@ static int test__sample_parsing(struct test_suite *test __maybe_unused, int subt
 	 * were added.  Please actually update the test rather than just change
 	 * the condition below.
 	 */
-	if (PERF_SAMPLE_MAX > PERF_SAMPLE_WEIGHT_STRUCT << 1) {
+	if (PERF_SAMPLE_MAX > PERF_SAMPLE_HOST_IP << 1) {
 		pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n");
 		return -1;
 	}
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 02233ef0eaff..cc5ce39ddacd 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -74,6 +74,33 @@ int build_id__mark_dso_hit(const struct perf_tool *tool __maybe_unused,
 
 	addr_location__exit(&al);
 
+	if (sample->host_ip) {
+		struct machine *host_machine = machine__is_host(machine) ?
+			machine : (machine->machines ? &machine->machines->host : NULL);
+
+		if (host_machine) {
+			struct thread *host_thread = machine__findnew_thread(host_machine,
+									     sample->pid,
+									     sample->tid);
+			struct maps *host_maps = NULL;
+
+			if (machine__kernel_ip(host_machine, sample->host_ip))
+				host_maps = machine__kernel_maps(host_machine);
+			else if (host_thread)
+				host_maps = thread__maps(host_thread);
+
+			if (host_maps) {
+				struct map *host_map = maps__find(host_maps, sample->host_ip);
+
+				if (host_map) {
+					dso__set_hit(map__dso(host_map));
+					map__put(host_map);
+				}
+			}
+			thread__put(host_thread);
+		}
+	}
+
 	sample__for_each_callchain_node(thread, sample, PERF_MAX_STACK_DEPTH,
 					/*symbols=*/false, mark_dso_hit_callback, /*data=*/NULL);
 
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 90269b8d1745..5950a383eff4 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1769,6 +1769,9 @@ void evsel__config(struct evsel *evsel, const struct record_opts *opts,
 	if (opts->sample_code_page_size)
 		evsel__set_sample_bit(evsel, CODE_PAGE_SIZE);
 
+	if (opts->sample_host_ip && !evsel__is_dummy_event(evsel))
+		evsel__set_sample_bit(evsel, HOST_IP);
+
 	if (opts->record_switch_events)
 		attr->context_switch = track;
 
@@ -2825,6 +2828,13 @@ static bool evsel__detect_missing_features(struct evsel *evsel, struct perf_cpu
 
 	/* Please add new feature detection here. */
 
+	attr.sample_type = PERF_SAMPLE_HOST_IP;
+	if (has_attr_feature(&attr, /*flags=*/0))
+		goto found;
+	perf_missing_features.host_ip = true;
+	pr_debug2_peo("Kernel has no PERF_SAMPLE_HOST_IP support\n");
+	attr.sample_type = 0;
+
 	attr.defer_callchain = true;
 	if (has_attr_feature(&attr, /*flags=*/0))
 		goto found;
@@ -3898,6 +3908,13 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,
 		array++;
 	}
 
+	data->host_ip = 0;
+	if (type & PERF_SAMPLE_HOST_IP) {
+		OVERFLOW_CHECK_u64(array);
+		data->host_ip = *array;
+		array++;
+	}
+
 	if (type & PERF_SAMPLE_AUX) {
 		OVERFLOW_CHECK_u64(array);
 		sz = *array++;
@@ -4677,6 +4694,11 @@ int evsel__open_strerror(struct evsel *evsel, struct target *target,
 							   size - printed);
 		break;
 	case EINVAL:
+		if ((evsel->core.attr.sample_type & PERF_SAMPLE_HOST_IP) &&
+		    perf_missing_features.host_ip) {
+			return scnprintf(msg, size,
+					 "Asking for the host IP isn't supported by this kernel.");
+		}
 		if (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE && perf_missing_features.code_page_size)
 			return scnprintf(msg, size, "Asking for the code page size isn't supported by this kernel.");
 		if (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE && perf_missing_features.data_page_size)
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 53f3641b66e1..ce38cea0f729 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -236,6 +236,7 @@ struct perf_missing_features {
 	bool aux_action;
 	bool inherit_sample_read;
 	bool defer_callchain;
+	bool host_ip;
 };
 
 extern struct perf_missing_features perf_missing_features;
diff --git a/tools/perf/util/perf_event_attr_fprintf.c b/tools/perf/util/perf_event_attr_fprintf.c
index 2e8be9a357dc..492dcc04bd99 100644
--- a/tools/perf/util/perf_event_attr_fprintf.c
+++ b/tools/perf/util/perf_event_attr_fprintf.c
@@ -40,7 +40,7 @@ static void __p_sample_type(char *buf, size_t size, u64 value)
 		bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
 		bit_name(WEIGHT), bit_name(PHYS_ADDR), bit_name(AUX),
 		bit_name(CGROUP), bit_name(DATA_PAGE_SIZE), bit_name(CODE_PAGE_SIZE),
-		bit_name(WEIGHT_STRUCT),
+		bit_name(WEIGHT_STRUCT), bit_name(HOST_IP),
 		{ .name = NULL, }
 	};
 #undef bit_name
@@ -384,6 +384,7 @@ int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
 		PRINT_ATTRf_bf(sigtrap, p_unsigned);
 		PRINT_ATTRf_bf(defer_callchain, p_unsigned);
 		PRINT_ATTRf_bf(defer_output, p_unsigned);
+		PRINT_ATTRf_bf(mmap_bpf, p_unsigned);
 	}
 
 	PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned, false);
diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h
index 411bb7276ad7..99f20019dbf8 100644
--- a/tools/perf/util/record.h
+++ b/tools/perf/util/record.h
@@ -23,6 +23,7 @@ struct record_opts {
 	bool	      sample_phys_addr;
 	bool	      sample_data_page_size;
 	bool	      sample_code_page_size;
+	bool	      sample_host_ip;
 	bool	      sample_weight;
 	bool	      sample_time;
 	bool	      sample_time_set;
diff --git a/tools/perf/util/sample.h b/tools/perf/util/sample.h
index cb4b16654876..c27f21db45e1 100644
--- a/tools/perf/util/sample.h
+++ b/tools/perf/util/sample.h
@@ -163,6 +163,8 @@ struct perf_sample {
 	u64 code_page_size;
 	/** @cgroup: The sample event PERF_SAMPLE_CGROUP value. */
 	u64 cgroup;
+	/** @host_ip: The sample event PERF_SAMPLE_HOST_IP value. */
+	u64 host_ip;
 	/** @file_offset: Byte offset of this event in the perf.data file. */
 	u64 file_offset;
 	/** @flags: Extra flag data from auxiliary events like intel-pt. */
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 56a5e57ec512..f143ffbd7f8b 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1734,6 +1734,9 @@ static void dump_sample(struct machine *machine, union perf_event *event,
 	if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)
 		printf(" .. code page size: %s\n", get_page_size_name(sample->code_page_size, str));
 
+	if (sample_type & PERF_SAMPLE_HOST_IP)
+		printf(" .. host_ip: %#" PRIx64 "\n", sample->host_ip);
+
 	if (sample_type & PERF_SAMPLE_TRANSACTION)
 		printf("... transaction: %" PRIx64 "\n", sample->transaction);
 
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index b9dbd105cdc3..92e9b086b1b3 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -1704,6 +1704,9 @@ size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
 	if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
 		result += sizeof(u64);
 
+	if (type & PERF_SAMPLE_HOST_IP)
+		result += sizeof(u64);
+
 	if (type & PERF_SAMPLE_AUX) {
 		result += sizeof(u64);
 		result += sample->aux_sample.size;
@@ -1958,6 +1961,11 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_fo
 		array++;
 	}
 
+	if (type & PERF_SAMPLE_HOST_IP) {
+		*array = sample->host_ip;
+		array++;
+	}
+
 	if (type & PERF_SAMPLE_AUX) {
 		sz = sample->aux_sample.size;
 		*array++ = sz;
-- 
2.56.1.915.g69d5ec7298-goog


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

Thread overview: 25+ 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-02  8:29   ` Christian Borntraeger
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 ` Ian Rogers [this message]
2026-12-01 23:36   ` [RFC PATCH 09/16] perf tools: Add PERF_SAMPLE_HOST_IP parsing, synthesis, and record --host-ip 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 ` [RFC PATCH 16/16] perf test: Add PERF_SAMPLE_HOST_IP symbolization, sorting, and ASLR tests Ian Rogers
2026-12-01 23:40   ` 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-10-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.