* [PATCH v3 01/19] perf/core: Protect per-CPU callchain buffer across bpf_overflow_handler()
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
@ 2026-11-16 7:43 ` Ian Rogers
2026-11-16 7:43 ` [PATCH v3 02/19] perf/uapi: Factor perf_build_id out into its own top-level struct Ian Rogers
` (17 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:43 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
In bpf_overflow_handler(), perf_prepare_sample() runs before bpf_prog_run()
and saves data->callchain pointing into the current CPU's callchain buffer
for the active interrupt_context_level(), after get_perf_callchain() has
already released its callchain_recursion slot.
If the attached BPF_PROG_TYPE_PERF_EVENT program invokes a helper at the
same interrupt_context_level() that calls get_callchain_entry() (such as
bpf_perf_event_output() on a PERF_COUNT_SW_BPF_OUTPUT event configured with
PERF_SAMPLE_CALLCHAIN and a larger stack, or bpf_get_task_stack()), it
re-acquires the same per-CPU callchain buffer and overwrites
data->callchain->nr in place. When bpf_prog_run() returns non-zero and
__perf_event_output() emits the outer event's sample, perf_output_begin()
reserves only the smaller dyn_size computed earlier while
perf_output_sample() copies the overwritten larger data->callchain->nr
entries, overflowing the ring-buffer reservation.
Hold the callchain recursion slot via get_callchain_entry() across
bpf_prog_run() when PERF_SAMPLE_CALLCHAIN has been prepared so nested
callers at the same context level cannot overwrite the active per-CPU
callchain buffer.
Fixes: 0eed28220598 ("perf/core: Call perf_prepare_sample() before running BPF")
Signed-off-by: Ian Rogers <irogers@google.com>
---
kernel/events/core.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 614f9f0e6500..fd9f654de269 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -10818,8 +10818,15 @@ static int bpf_overflow_handler(struct perf_event *event,
rcu_read_lock();
prog = READ_ONCE(event->prog);
if (prog) {
+ struct perf_callchain_entry *callchain = NULL;
+ int callchain_rctx;
+
perf_prepare_sample(data, event, regs);
+ if (data->sample_flags & PERF_SAMPLE_CALLCHAIN)
+ callchain = get_callchain_entry(&callchain_rctx);
ret = bpf_prog_run(prog, &ctx);
+ if (callchain)
+ put_callchain_entry(callchain_rctx);
}
rcu_read_unlock();
out:
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 02/19] perf/uapi: Factor perf_build_id out into its own top-level struct
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
2026-11-16 7:43 ` [PATCH v3 01/19] perf/core: Protect per-CPU callchain buffer across bpf_overflow_handler() Ian Rogers
@ 2026-11-16 7:43 ` Ian Rogers
2026-11-16 7:43 ` [PATCH v3 03/19] perf/uapi: Add BUILD_ID_OFFSET sample types to UAPI Ian Rogers
` (16 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:43 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Document struct perf_build_id as a top-level structure in the perf_event
UAPI comment and add it to the PERF_RECORD_MMAP2 union comment so future
sample types can reference struct perf_build_id directly.
Signed-off-by: Ian Rogers <irogers@google.com>
---
include/uapi/linux/perf_event.h | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index c49fc76292f7..deb7c42d109b 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -1099,6 +1099,18 @@ enum perf_event_type {
*/
PERF_RECORD_SAMPLE = 9,
+ /*
+ * Build IDs may be present in a number of events. They have a
+ * consistent encoding of:
+ *
+ * struct perf_build_id {
+ * u8 size;
+ * u8 __reserved_1;
+ * u16 __reserved_2;
+ * u8 data[20];
+ * };
+ */
+
/*
* The MMAP2 records are an augmented version of MMAP, they add
* maj, min, ino numbers to be used to uniquely identify each mapping
@@ -1123,6 +1135,7 @@ enum perf_event_type {
* u16 __reserved_2;
* u8 build_id[20];
* };
+ * struct perf_build_id bid;
* };
* u32 prot, flags;
* char filename[];
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 03/19] perf/uapi: Add BUILD_ID_OFFSET sample types to UAPI
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
2026-11-16 7:43 ` [PATCH v3 01/19] perf/core: Protect per-CPU callchain buffer across bpf_overflow_handler() Ian Rogers
2026-11-16 7:43 ` [PATCH v3 02/19] perf/uapi: Factor perf_build_id out into its own top-level struct Ian Rogers
@ 2026-11-16 7:43 ` Ian Rogers
2026-11-16 7:43 ` [PATCH v3 04/19] bpf: Export stack_map_get_build_id_offset() for perf events Ian Rogers
` (15 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:43 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Introduce PERF_SAMPLE_BUILD_ID_OFFSET and
PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET in enum perf_event_sample_format.
For PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET (and
PERF_RECORD_CALLCHAIN_DEFERRED when PERF_RECORD_MISC_MMAP_BUILD_ID is
set), encode a deduplicated table of variable-length build IDs
(u32 nr_build_ids, u32 nr_offsets, followed by packed { u8 size;
u8 data[size]; } entries padded to 8-byte alignment) and an array of
64-bit offsets whose top 8 bits encode the 1-based build ID index (or
0x00 for unresolved user addresses and 0xff for kernel/context entries).
In common cases where multiple callchain frames belong to the same
binary, each unique build ID is encoded only once.
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Ian Rogers <irogers@google.com>
---
include/uapi/linux/perf_event.h | 59 ++++++++++++++++++++++++++++++---
1 file changed, 54 insertions(+), 5 deletions(-)
diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index deb7c42d109b..775420ed9aee 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -165,8 +165,10 @@ enum perf_event_sample_format {
PERF_SAMPLE_DATA_PAGE_SIZE = 1U << 22,
PERF_SAMPLE_CODE_PAGE_SIZE = 1U << 23,
PERF_SAMPLE_WEIGHT_STRUCT = 1U << 24,
+ PERF_SAMPLE_BUILD_ID_OFFSET = 1U << 25,
+ PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET = 1U << 26,
- PERF_SAMPLE_MAX = 1U << 25, /* non-ABI */
+ PERF_SAMPLE_MAX = 1U << 27, /* non-ABI */
};
#define PERF_SAMPLE_WEIGHT_TYPE (PERF_SAMPLE_WEIGHT | PERF_SAMPLE_WEIGHT_STRUCT)
@@ -840,7 +842,8 @@ struct perf_event_mmap_page {
*
* PERF_RECORD_MISC_EXACT_IP - PERF_RECORD_SAMPLE of precise events
* PERF_RECORD_MISC_SWITCH_OUT_PREEMPT - PERF_RECORD_SWITCH* events
- * PERF_RECORD_MISC_MMAP_BUILD_ID - PERF_RECORD_MMAP2 event
+ * PERF_RECORD_MISC_MMAP_BUILD_ID - PERF_RECORD_MMAP2 and
+ * PERF_RECORD_CALLCHAIN_DEFERRED events
*
*
* PERF_RECORD_MISC_EXACT_IP:
@@ -852,7 +855,7 @@ struct perf_event_mmap_page {
* Indicates that thread was preempted in TASK_RUNNING state.
*
* PERF_RECORD_MISC_MMAP_BUILD_ID:
- * Indicates that mmap2 event carries build ID data.
+ * Indicates that mmap2 or deferred callchain event carries build ID data.
*/
#define PERF_RECORD_MISC_EXACT_IP (1 << 14)
#define PERF_RECORD_MISC_SWITCH_OUT_PREEMPT (1 << 14)
@@ -1095,6 +1098,38 @@ enum perf_event_type {
* { u64 code_page_size;} && PERF_SAMPLE_CODE_PAGE_SIZE
* { u64 size;
* char data[size]; } && PERF_SAMPLE_AUX
+ * { union {
+ * struct {
+ * struct perf_build_id bid;
+ * u64 offset;
+ * };
+ * struct {
+ * u64 zero;
+ * u64 context;
+ * u64 cookie;
+ * u64 ip;
+ * }; && attr.defer_callchain
+ * }; } && PERF_SAMPLE_BUILD_ID_OFFSET
+ * { u32 nr_build_ids;
+ * u32 nr_offsets;
+ * struct {
+ * u8 size;
+ * u8 data[size];
+ * }[nr_build_ids];
+ * u8 pad[];
+ * u64 offsets[nr_offsets];
+ * #
+ * # Entries in offsets[] include PERF_CONTEXT_* markers (which
+ * # have top 8 bits 0xff). Kernel/hypervisor entries (after
+ * # PERF_CONTEXT_KERNEL, PERF_CONTEXT_HV, PERF_CONTEXT_GUEST,
+ * # or PERF_CONTEXT_GUEST_KERNEL) and the cookie word following
+ * # PERF_CONTEXT_USER_DEFERRED are raw 64-bit values. User
+ * # entries (after PERF_CONTEXT_USER or PERF_CONTEXT_GUEST_USER,
+ * # or in PERF_RECORD_CALLCHAIN_DEFERRED) use the top 8 bits to
+ * # encode a 1-based index (1..254) into the build_ids table and
+ * # the lower 56 bits for the DSO file offset, or 0x00 for an
+ * # unresolved user virtual address.
+ * } && PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET
* };
*/
PERF_RECORD_SAMPLE = 9,
@@ -1306,8 +1341,22 @@ enum perf_event_type {
* struct {
* struct perf_event_header header;
* u64 cookie;
- * u64 nr;
- * u64 ips[nr];
+ * union {
+ * struct {
+ * u64 nr;
+ * u64 ips[nr];
+ * };
+ * struct {
+ * u32 nr_build_ids;
+ * u32 nr_offsets;
+ * struct {
+ * u8 size;
+ * u8 data[size];
+ * }[nr_build_ids];
+ * u8 pad[];
+ * u64 offsets[nr_offsets];
+ * }; && (header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID)
+ * };
* struct sample_id sample_id;
* };
*/
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 04/19] bpf: Export stack_map_get_build_id_offset() for perf events
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (2 preceding siblings ...)
2026-11-16 7:43 ` [PATCH v3 03/19] perf/uapi: Add BUILD_ID_OFFSET sample types to UAPI Ian Rogers
@ 2026-11-16 7:43 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 05/19] perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET sample types Ian Rogers
` (14 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:43 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Make stack_map_get_build_id_offset() non-static and declare it in
include/linux/bpf.h (with an inline fallback when CONFIG_BPF_SYSCALL or
CONFIG_PERF_EVENTS is disabled) so that the perf events core can reuse
it to resolve user-space instruction pointers to build IDs and file
offsets.
Signed-off-by: Ian Rogers <irogers@google.com>
---
include/linux/bpf.h | 14 ++++++++++++++
kernel/bpf/stackmap.c | 4 ++--
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 31ff71d829b9..9b2ca352ac6c 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -4503,4 +4503,18 @@ static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u64 all
return 0;
}
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PERF_EVENTS)
+void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
+ u32 trace_nr, bool user, bool may_fault);
+#else
+static inline void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
+ u32 trace_nr, bool user, bool may_fault)
+{
+ for (u32 i = 0; i < trace_nr; i++) {
+ id_offs[i].status = BPF_STACK_BUILD_ID_IP;
+ memset(id_offs[i].build_id, 0, BPF_BUILD_ID_SIZE);
+ }
+}
+#endif
+
#endif /* _LINUX_BPF_H */
diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index a19126f5db02..11f6349b2b8d 100644
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -406,8 +406,8 @@ static void stack_map_get_build_id_offset_sleepable(struct bpf_stack_build_id *i
* id_offs[i].build_id is zeroed out and id_offs[i].status is set to
* BPF_STACK_BUILD_ID_IP.
*/
-static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
- u32 trace_nr, bool user, bool may_fault)
+void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
+ u32 trace_nr, bool user, bool may_fault)
{
struct mmap_unlock_irq_work *work;
bool has_user_ctx = user && current && current->mm;
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 05/19] perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET sample types
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (3 preceding siblings ...)
2026-11-16 7:43 ` [PATCH v3 04/19] bpf: Export stack_map_get_build_id_offset() for perf events Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 06/19] perf/core: Support deferred unwinding for BUILD_ID_OFFSET " Ian Rogers
` (13 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Implement synchronous PERF_SAMPLE_BUILD_ID_OFFSET and
PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET payload writing in the perf event
core using stack_map_get_build_id_offset(..., may_fault=false).
For callchains, extend the per-CPU callchain buffers in
kernel/events/callchain.c to provide scratch space to deduplicate build
IDs and encode the compressed build ID table and tagged 64-bit offsets
(with the top 8 bits storing the 1-based build ID index).
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Ian Rogers <irogers@google.com>
---
arch/powerpc/perf/core-book3s.c | 10 +-
arch/s390/kernel/perf_cpum_sf.c | 7 +-
include/linux/perf_event.h | 43 ++++++-
kernel/bpf/stackmap.c | 6 +-
kernel/events/callchain.c | 206 +++++++++++++++++++++++++++++++-
kernel/events/core.c | 132 ++++++++++++++++++--
6 files changed, 385 insertions(+), 19 deletions(-)
diff --git a/arch/powerpc/perf/core-book3s.c b/arch/powerpc/perf/core-book3s.c
index 10ded865583c..bc51ab537851 100644
--- a/arch/powerpc/perf/core-book3s.c
+++ b/arch/powerpc/perf/core-book3s.c
@@ -2271,10 +2271,11 @@ static void record_and_restart(struct perf_event *event, unsigned long val,
/*
* If address is not requested in the sample via
- * PERF_SAMPLE_IP, just record that sample irrespective
- * of SIAR valid check.
+ * PERF_SAMPLE_IP or PERF_SAMPLE_BUILD_ID_OFFSET, just
+ * record that sample irrespective of SIAR valid check.
*/
- if (event->attr.sample_type & PERF_SAMPLE_IP)
+ if (event->attr.sample_type &
+ (PERF_SAMPLE_IP | PERF_SAMPLE_BUILD_ID_OFFSET))
record = siar_valid(regs);
else
record = 1;
@@ -2297,7 +2298,8 @@ static void record_and_restart(struct perf_event *event, unsigned long val,
* these cases.
*/
if (event->attr.exclude_kernel &&
- (event->attr.sample_type & PERF_SAMPLE_IP) &&
+ (event->attr.sample_type &
+ (PERF_SAMPLE_IP | PERF_SAMPLE_BUILD_ID_OFFSET)) &&
is_kernel_addr(mfspr(SPRN_SIAR)))
record = 0;
diff --git a/arch/s390/kernel/perf_cpum_sf.c b/arch/s390/kernel/perf_cpum_sf.c
index 7bfeb5208177..30b0b6ba5b41 100644
--- a/arch/s390/kernel/perf_cpum_sf.c
+++ b/arch/s390/kernel/perf_cpum_sf.c
@@ -840,8 +840,11 @@ static bool is_callchain_event(struct perf_event *event)
{
u64 sample_type = event->attr.sample_type;
- return sample_type & (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_REGS_USER |
- PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_STACK_USER);
+ return sample_type & (PERF_SAMPLE_CALLCHAIN |
+ PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET |
+ PERF_SAMPLE_BUILD_ID_OFFSET |
+ PERF_SAMPLE_REGS_USER | PERF_SAMPLE_REGS_INTR |
+ PERF_SAMPLE_STACK_USER);
}
static int cpumsf_pmu_event_init(struct perf_event *event)
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index e1e11500df09..9b5d9613a6f3 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -61,6 +61,31 @@ struct perf_callchain_entry {
u64 ip[]; /* /proc/sys/kernel/perf_event_max_stack */
};
+#define PERF_CALLCHAIN_BUILD_ID_MAX_NRS 254
+#define PERF_CALLCHAIN_BUILD_ID_MAX_SIZE 20
+#define PERF_CALLCHAIN_BUILD_ID_TABLE_MAX_SIZE \
+ ALIGN(PERF_CALLCHAIN_BUILD_ID_MAX_NRS * (1 + PERF_CALLCHAIN_BUILD_ID_MAX_SIZE), sizeof(u64))
+
+struct perf_sample_build_id_offset {
+ u8 size;
+ u8 res1;
+ u16 res2;
+ u8 build_id[PERF_CALLCHAIN_BUILD_ID_MAX_SIZE];
+ u64 offset;
+};
+
+struct perf_callchain_build_id_entry {
+ u32 build_ids_len;
+ bool has_ip_bid;
+ struct perf_sample_build_id_offset ip_bid;
+ struct {
+ u32 nr_build_ids;
+ u32 nr_offsets;
+ u8 build_ids[PERF_CALLCHAIN_BUILD_ID_TABLE_MAX_SIZE];
+ };
+ u64 offsets[]; /* /proc/sys/kernel/perf_event_max_stack */
+};
+
struct perf_callchain_entry_ctx {
struct perf_callchain_entry *entry;
u32 max_stack;
@@ -1347,6 +1372,7 @@ struct perf_sample_data {
u64 data_page_size;
u64 code_page_size;
u64 aux_size;
+ struct perf_callchain_build_id_entry *callchain_build_id;
} ____cacheline_aligned;
/* default value for data source */
@@ -1377,7 +1403,8 @@ static inline void perf_sample_save_callchain(struct perf_sample_data *data,
{
int size = 1;
- if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN))
+ if (!(event->attr.sample_type &
+ (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET)))
return;
if (WARN_ON_ONCE(data->sample_flags & PERF_SAMPLE_CALLCHAIN))
return;
@@ -1385,7 +1412,8 @@ static inline void perf_sample_save_callchain(struct perf_sample_data *data,
data->callchain = perf_callchain(event, regs);
size += data->callchain->nr;
- data->dyn_size += size * sizeof(u64);
+ if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
+ data->dyn_size += size * sizeof(u64);
data->sample_flags |= PERF_SAMPLE_CALLCHAIN;
}
@@ -1747,6 +1775,17 @@ extern int get_callchain_buffers(int max_stack);
extern void put_callchain_buffers(void);
extern struct perf_callchain_entry *get_callchain_entry(int *rctx);
extern void put_callchain_entry(int rctx);
+struct bpf_stack_build_id;
+extern struct perf_callchain_build_id_entry *
+get_callchain_build_id_entry(int *rctx, struct bpf_stack_build_id **bpf_bids);
+extern void perf_callchain_encode_build_ids(struct perf_callchain_build_id_entry *entry,
+ const u64 *ips, u32 nr, u32 user_start,
+ const struct bpf_stack_build_id *bpf_bids,
+ u32 nr_bpf_bids, u32 max_size);
+extern struct perf_callchain_build_id_entry *
+perf_callchain_build_id(struct perf_event *event,
+ struct perf_callchain_entry *callchain,
+ u64 ip, bool resolve_ip, u32 max_size);
extern int sysctl_perf_event_max_stack;
extern int sysctl_perf_event_max_contexts_per_stack;
diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index 11f6349b2b8d..1f4a0c425af6 100644
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -691,7 +691,8 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
int ret;
/* perf_sample_data doesn't have callchain, use bpf_get_stackid */
- if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN))
+ if (!(event->attr.sample_type &
+ (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET)))
return bpf_get_stackid((unsigned long)(ctx->regs),
(unsigned long) map, flags, 0, 0);
@@ -982,7 +983,8 @@ BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
int err = -EINVAL;
__u64 nr_kernel;
- if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN))
+ if (!(event->attr.sample_type &
+ (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET)))
return __bpf_get_stack(regs, NULL, buf, size, flags, false /* !may_fault */);
if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
diff --git a/kernel/events/callchain.c b/kernel/events/callchain.c
index 9d24b6e0c91f..935b34d2dd33 100644
--- a/kernel/events/callchain.c
+++ b/kernel/events/callchain.c
@@ -9,6 +9,7 @@
*/
#include <linux/perf_event.h>
+#include <linux/bpf.h>
#include <linux/slab.h>
#include <linux/sched/task_stack.h>
#include <linux/uprobes.h>
@@ -31,6 +32,26 @@ static inline size_t perf_callchain_entry__sizeof(void)
sysctl_perf_event_max_contexts_per_stack));
}
+static inline size_t perf_callchain_build_id_entry__sizeof(void)
+{
+ return (sizeof(struct perf_callchain_build_id_entry) +
+ sizeof(__u64) * (sysctl_perf_event_max_stack +
+ sysctl_perf_event_max_contexts_per_stack));
+}
+
+static inline size_t perf_callchain_bpf_build_id__sizeof(void)
+{
+ return sizeof(struct bpf_stack_build_id) *
+ (sysctl_perf_event_max_stack + 1);
+}
+
+static inline size_t perf_callchain_context__sizeof(void)
+{
+ return perf_callchain_entry__sizeof() +
+ perf_callchain_build_id_entry__sizeof() +
+ perf_callchain_bpf_build_id__sizeof();
+}
+
static DEFINE_PER_CPU(u8, callchain_recursion[PERF_NR_CONTEXTS]);
static atomic_t nr_callchain_events;
static DEFINE_MUTEX(callchain_mutex);
@@ -86,7 +107,7 @@ static int alloc_callchain_buffers(void)
if (!entries)
return -ENOMEM;
- size = perf_callchain_entry__sizeof() * PERF_NR_CONTEXTS;
+ size = perf_callchain_context__sizeof() * PERF_NR_CONTEXTS;
for_each_possible_cpu(cpu) {
entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
@@ -169,7 +190,22 @@ struct perf_callchain_entry *get_callchain_entry(int *rctx)
cpu = smp_processor_id();
return (((void *)entries->cpu_entries[cpu]) +
- (*rctx * perf_callchain_entry__sizeof()));
+ (*rctx * perf_callchain_context__sizeof()));
+}
+
+struct perf_callchain_build_id_entry *
+get_callchain_build_id_entry(int *rctx, struct bpf_stack_build_id **bpf_bids)
+{
+ void *base = get_callchain_entry(rctx);
+
+ if (!base)
+ return NULL;
+
+ base += perf_callchain_entry__sizeof();
+ if (bpf_bids)
+ *bpf_bids = base + perf_callchain_build_id_entry__sizeof();
+
+ return base;
}
void
@@ -178,6 +214,172 @@ put_callchain_entry(int rctx)
put_recursion_context(this_cpu_ptr(callchain_recursion), rctx);
}
+void perf_callchain_encode_build_ids(struct perf_callchain_build_id_entry *entry,
+ const u64 *ips, u32 nr, u32 user_start,
+ const struct bpf_stack_build_id *bpf_bids,
+ u32 nr_bpf_bids, u32 max_size)
+{
+ u32 nr_build_ids = 0, nr_offsets = 0;
+ u32 raw_bids_len = 0, aligned_bids_len = 0;
+
+ static_assert(BUILD_ID_SIZE_MAX == PERF_CALLCHAIN_BUILD_ID_MAX_SIZE);
+
+ for (u32 i = 0; i < nr; i++) {
+ u64 ip = ips[i];
+ u64 encoded_offset;
+ const struct bpf_stack_build_id *bpf_bid = NULL;
+
+ if (i >= user_start && i - user_start < nr_bpf_bids)
+ bpf_bid = &bpf_bids[i - user_start];
+
+ if (bpf_bid && bpf_bid->status == BPF_STACK_BUILD_ID_VALID) {
+ u32 pos = 0, found_idx = 0;
+
+ for (u32 b = 0; b < nr_build_ids; b++) {
+ u8 sz = entry->build_ids[pos];
+
+ if (sz == BUILD_ID_SIZE_MAX &&
+ !memcmp(&entry->build_ids[pos + 1],
+ bpf_bid->build_id,
+ BUILD_ID_SIZE_MAX)) {
+ found_idx = b + 1;
+ break;
+ }
+ pos += 1 + sz;
+ }
+
+ if (!found_idx &&
+ nr_build_ids < PERF_CALLCHAIN_BUILD_ID_MAX_NRS) {
+ u32 next_raw_len = raw_bids_len + 1 + BUILD_ID_SIZE_MAX;
+ u32 next_aligned_len = ALIGN(next_raw_len, sizeof(u64));
+
+ if (next_aligned_len + (i + 1) * sizeof(u64) > max_size)
+ break;
+ entry->build_ids[raw_bids_len] = BUILD_ID_SIZE_MAX;
+ memcpy(&entry->build_ids[raw_bids_len + 1],
+ bpf_bid->build_id, BUILD_ID_SIZE_MAX);
+ nr_build_ids++;
+ raw_bids_len = next_raw_len;
+ aligned_bids_len = next_aligned_len;
+ found_idx = nr_build_ids;
+ }
+
+ if (aligned_bids_len + (i + 1) * sizeof(u64) > max_size)
+ break;
+
+ if (found_idx) {
+ encoded_offset = ((u64)found_idx << 56) |
+ (bpf_bid->offset & GENMASK_ULL(55, 0));
+ } else {
+ encoded_offset = ip & GENMASK_ULL(55, 0);
+ }
+ } else {
+ if (aligned_bids_len + (i + 1) * sizeof(u64) > max_size)
+ break;
+ if (i >= user_start && ip < PERF_CONTEXT_MAX)
+ encoded_offset = ip & GENMASK_ULL(55, 0);
+ else
+ encoded_offset = ip;
+ }
+
+ entry->offsets[i] = encoded_offset;
+ nr_offsets = i + 1;
+ }
+
+ if (nr_offsets > 0 && nr_offsets <= user_start &&
+ entry->offsets[nr_offsets - 1] == PERF_CONTEXT_USER_DEFERRED &&
+ (nr_offsets < 2 ||
+ entry->offsets[nr_offsets - 2] != PERF_CONTEXT_USER_DEFERRED))
+ nr_offsets--;
+
+ if (aligned_bids_len > raw_bids_len) {
+ memset(&entry->build_ids[raw_bids_len], 0,
+ aligned_bids_len - raw_bids_len);
+ }
+ entry->nr_build_ids = nr_build_ids;
+ entry->nr_offsets = nr_offsets;
+ entry->build_ids_len = aligned_bids_len;
+}
+
+static struct perf_callchain_build_id_entry __empty_callchain_build_id = {};
+
+struct perf_callchain_build_id_entry *
+perf_callchain_build_id(struct perf_event *event,
+ struct perf_callchain_entry *callchain,
+ u64 ip, bool resolve_ip, u32 max_size)
+{
+ bool crosstask = event->ctx->task && event->ctx->task != current;
+ bool has_user_ctx = is_user_task(current) && current->mm && !crosstask;
+ struct perf_callchain_build_id_entry *entry;
+ struct bpf_stack_build_id *bpf_bids;
+ u32 nr, user_start, nr_bpf_bids = 0, ip_slot = 0;
+ int rctx;
+
+ if (!callchain || !callchain->nr || max_size < sizeof(u64))
+ return &__empty_callchain_build_id;
+
+ entry = get_callchain_build_id_entry(&rctx, &bpf_bids);
+ if (!entry)
+ return &__empty_callchain_build_id;
+
+ entry->has_ip_bid = false;
+ nr = min_t(u64, callchain->nr,
+ sysctl_perf_event_max_stack +
+ sysctl_perf_event_max_contexts_per_stack);
+ user_start = nr;
+ if (has_user_ctx) {
+ for (u32 i = 0; i < nr; i++) {
+ if (callchain->ip[i] == PERF_CONTEXT_USER) {
+ user_start = i + 1;
+ break;
+ }
+ }
+ if (user_start < nr) {
+ nr_bpf_bids = min_t(u32, nr - user_start,
+ sysctl_perf_event_max_stack);
+ nr_bpf_bids = min_t(u32, nr_bpf_bids,
+ max_size / sizeof(u64));
+ }
+ if (nr_bpf_bids > 0) {
+ if (resolve_ip) {
+ bpf_bids[0].ip = ip;
+ ip_slot = 1;
+ }
+ for (u32 i = 0; i < nr_bpf_bids; i++) {
+ bpf_bids[ip_slot + i].ip =
+ callchain->ip[user_start + i];
+ }
+ stack_map_get_build_id_offset(bpf_bids,
+ ip_slot + nr_bpf_bids,
+ true, false);
+ if (resolve_ip) {
+ memset(&entry->ip_bid, 0,
+ sizeof(entry->ip_bid));
+ if (bpf_bids[0].status ==
+ BPF_STACK_BUILD_ID_VALID) {
+ entry->ip_bid.size =
+ BUILD_ID_SIZE_MAX;
+ memcpy(entry->ip_bid.build_id,
+ bpf_bids[0].build_id,
+ BUILD_ID_SIZE_MAX);
+ entry->ip_bid.offset =
+ bpf_bids[0].offset;
+ } else {
+ entry->ip_bid.offset = ip;
+ }
+ entry->has_ip_bid = true;
+ }
+ }
+ }
+
+ perf_callchain_encode_build_ids(entry, callchain->ip, nr, user_start,
+ bpf_bids + ip_slot, nr_bpf_bids,
+ max_size);
+ put_callchain_entry(rctx);
+
+ return entry;
+}
+
static void fixup_uretprobe_trampoline_entries(struct perf_callchain_entry *entry,
int start_entry_idx)
{
diff --git a/kernel/events/core.c b/kernel/events/core.c
index fd9f654de269..d38ffe52c301 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2096,6 +2096,9 @@ static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)
size += sizeof(data->code_page_size);
+ if (sample_type & PERF_SAMPLE_BUILD_ID_OFFSET)
+ size += sizeof(struct perf_sample_build_id_offset);
+
event->header_size = size;
}
@@ -8396,6 +8399,78 @@ static void perf_output_read(struct perf_output_handle *handle,
perf_output_read_one(handle, event, enabled, running);
}
+static void perf_bpf_to_sample_bid_offset(struct bpf_stack_build_id *bpf_bid)
+{
+ struct perf_sample_build_id_offset *bid = (void *)bpf_bid;
+ u8 size = 0;
+
+ static_assert(sizeof(*bid) == sizeof(*bpf_bid));
+ static_assert(offsetof(struct perf_sample_build_id_offset, build_id) ==
+ offsetof(struct bpf_stack_build_id, build_id));
+ static_assert(offsetof(struct perf_sample_build_id_offset, offset) ==
+ offsetof(struct bpf_stack_build_id, offset));
+
+ if (bpf_bid->status == BPF_STACK_BUILD_ID_VALID)
+ size = BUILD_ID_SIZE_MAX;
+ else
+ memset(bid->build_id, 0, sizeof(bid->build_id));
+
+ bid->size = size;
+ bid->res1 = 0;
+ bid->res2 = 0;
+}
+
+static void perf_output_sample_build_id(struct perf_output_handle *handle,
+ struct perf_event_header *header,
+ struct perf_sample_data *data,
+ struct perf_event *event)
+{
+ u64 sample_type = data->type;
+ bool sample_bid = sample_type & PERF_SAMPLE_BUILD_ID_OFFSET;
+ bool callchain_bid = sample_type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET;
+ bool crosstask = event->ctx->task && event->ctx->task != current;
+ bool has_user_ctx = is_user_task(current) && current->mm && !crosstask;
+ bool is_user = (header->misc & PERF_RECORD_MISC_CPUMODE_MASK) ==
+ PERF_RECORD_MISC_USER;
+
+ if (sample_bid) {
+ if (is_user && has_user_ctx) {
+ if (callchain_bid && data->callchain_build_id &&
+ data->callchain_build_id->has_ip_bid) {
+ perf_output_put(handle,
+ data->callchain_build_id->ip_bid);
+ } else {
+ struct bpf_stack_build_id bpf_bid = {
+ .ip = data->ip,
+ };
+
+ stack_map_get_build_id_offset(&bpf_bid, 1, true,
+ false);
+ perf_bpf_to_sample_bid_offset(&bpf_bid);
+ perf_output_put(handle, bpf_bid);
+ }
+ } else {
+ struct perf_sample_build_id_offset bid_offset = {
+ .offset = data->ip,
+ };
+
+ perf_output_put(handle, bid_offset);
+ }
+ }
+
+ if (callchain_bid) {
+ struct perf_callchain_build_id_entry *entry =
+ data->callchain_build_id;
+
+ __output_copy(handle, &entry->nr_build_ids,
+ sizeof(u64) + entry->build_ids_len);
+ if (entry->nr_offsets) {
+ __output_copy(handle, entry->offsets,
+ entry->nr_offsets * sizeof(u64));
+ }
+ }
+}
+
void perf_output_sample(struct perf_output_handle *handle,
struct perf_event_header *header,
struct perf_sample_data *data,
@@ -8589,6 +8664,10 @@ void perf_output_sample(struct perf_output_handle *handle,
perf_aux_sample_output(event, handle, data);
}
+ if (sample_type &
+ (PERF_SAMPLE_BUILD_ID_OFFSET | PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET))
+ perf_output_sample_build_id(handle, header, data, event);
+
if (!event->attr.watermark) {
int wakeup_events = event->attr.wakeup_events;
@@ -8804,7 +8883,9 @@ void perf_prepare_sample(struct perf_sample_data *data,
* sample flags that have already been done by the PMU driver.
*/
filtered_sample_type = sample_type;
- filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_CODE_PAGE_SIZE,
+ filtered_sample_type |= __cond_set(sample_type,
+ PERF_SAMPLE_CODE_PAGE_SIZE |
+ PERF_SAMPLE_BUILD_ID_OFFSET,
PERF_SAMPLE_IP);
filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_DATA_PAGE_SIZE |
PERF_SAMPLE_PHYS_ADDR, PERF_SAMPLE_ADDR);
@@ -8825,8 +8906,14 @@ void perf_prepare_sample(struct perf_sample_data *data,
data->sample_flags |= PERF_SAMPLE_IP;
}
- if (filtered_sample_type & PERF_SAMPLE_CALLCHAIN)
- perf_sample_save_callchain(data, event, regs);
+ if (filtered_sample_type &
+ (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET)) {
+ if (!(data->sample_flags & PERF_SAMPLE_CALLCHAIN))
+ perf_sample_save_callchain(data, event, regs);
+ if (filtered_sample_type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET)
+ data->dyn_size += sizeof(u64);
+ }
+
if (filtered_sample_type & PERF_SAMPLE_RAW) {
data->raw = NULL;
@@ -8986,6 +9073,24 @@ void perf_prepare_sample(struct perf_sample_data *data,
data->dyn_size += size + sizeof(u64); /* size above */
data->sample_flags |= PERF_SAMPLE_AUX;
}
+
+ if (filtered_sample_type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) {
+ u32 header_size = perf_sample_data_size(data, event);
+ u32 max_size = 0;
+ struct perf_callchain_build_id_entry *entry;
+
+ if (header_size < U16_MAX)
+ max_size = round_down(U16_MAX - header_size, sizeof(u64));
+
+ entry = perf_callchain_build_id(event, data->callchain,
+ data->ip,
+ !!(sample_type & PERF_SAMPLE_BUILD_ID_OFFSET),
+ max_size);
+ data->callchain_build_id = entry;
+ data->dyn_size += entry->build_ids_len +
+ entry->nr_offsets * sizeof(u64);
+ data->sample_flags |= PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET;
+ }
}
void perf_prepare_header(struct perf_event_header *header,
@@ -10822,7 +10927,8 @@ static int bpf_overflow_handler(struct perf_event *event,
int callchain_rctx;
perf_prepare_sample(data, event, regs);
- if (data->sample_flags & PERF_SAMPLE_CALLCHAIN)
+ if (data->sample_flags &
+ (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET))
callchain = get_callchain_entry(&callchain_rctx);
ret = bpf_prog_run(prog, &ctx);
if (callchain)
@@ -10851,7 +10957,8 @@ static inline int perf_event_set_bpf_handler(struct perf_event *event,
if (event->attr.precise_ip &&
prog->call_get_stack &&
- (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) ||
+ (!(event->attr.sample_type &
+ (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET)) ||
event->attr.exclude_callchain_kernel ||
event->attr.exclude_callchain_user)) {
/*
@@ -13713,7 +13820,8 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
}
if (!event->parent) {
- if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
+ if (event->attr.sample_type &
+ (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET)) {
err = get_callchain_buffers(attr->sample_max_stack);
if (err)
return ERR_PTR(err);
@@ -13868,6 +13976,15 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
if (attr->sample_type & PERF_SAMPLE_CGROUP)
return -EINVAL;
#endif
+#ifndef CONFIG_BPF_SYSCALL
+ if (attr->sample_type &
+ (PERF_SAMPLE_BUILD_ID_OFFSET | PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET))
+ return -EINVAL;
+#endif
+ if (attr->defer_callchain &&
+ (attr->sample_type & PERF_SAMPLE_CALLCHAIN) &&
+ (attr->sample_type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET))
+ return -EINVAL;
if ((attr->sample_type & PERF_SAMPLE_WEIGHT) &&
(attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT))
return -EINVAL;
@@ -14096,7 +14213,8 @@ SYSCALL_DEFINE5(perf_event_open,
return err;
if (!attr.exclude_kernel || attr.text_poke ||
- ((attr.sample_type & PERF_SAMPLE_CALLCHAIN) &&
+ ((attr.sample_type &
+ (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET)) &&
!attr.exclude_callchain_kernel)) {
err = perf_allow_kernel();
if (err)
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 06/19] perf/core: Support deferred unwinding for BUILD_ID_OFFSET sample types
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (4 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 05/19] perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET sample types Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 07/19] perf inject: Set ordered_events and ordering_requires_timestamps for --aslr Ian Rogers
` (12 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
When attr.defer_callchain is set on events using
PERF_SAMPLE_BUILD_ID_OFFSET or PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET,
request a deferred user-space unwind via unwind_deferred_request() and
emit a PERF_CONTEXT_USER_DEFERRED marker and cookie in the sample.
In the faultable task_work callback (perf_unwind_deferred_callback()),
resolve the unwound user IPs to build IDs and file offsets using
stack_map_get_build_id_offset(..., may_fault=true), encode them into the
compressed build ID + offset format, and emit
PERF_RECORD_CALLCHAIN_DEFERRED with PERF_RECORD_MISC_MMAP_BUILD_ID set.
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Ian Rogers <irogers@google.com>
---
kernel/events/core.c | 175 ++++++++++++++++++++++++++++++++++++++++---
1 file changed, 164 insertions(+), 11 deletions(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index d38ffe52c301..ef40c4442e82 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -460,6 +460,8 @@ static atomic_t nr_bpf_events __read_mostly;
static atomic_t nr_cgroup_events __read_mostly;
static atomic_t nr_text_poke_events __read_mostly;
static atomic_t nr_build_id_events __read_mostly;
+static atomic_t nr_defer_output_build_id_events __read_mostly;
+static atomic_t nr_defer_output_callchain_build_id_events __read_mostly;
static LIST_HEAD(pmus);
static DEFINE_MUTEX(pmus_lock);
@@ -5639,6 +5641,25 @@ static void detach_perf_ctx_data(struct perf_event *event)
}
}
+static bool has_defer_output_callchain_build_id_offset(const struct perf_event *event)
+{
+ return event->attr.defer_output &&
+ (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) &&
+ !(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) &&
+ !event->attr.exclude_callchain_user;
+}
+
+static bool has_defer_output_build_id_offset(const struct perf_event *event)
+{
+ if (!event->attr.defer_output)
+ return false;
+ if (has_defer_output_callchain_build_id_offset(event))
+ return true;
+ return (event->attr.sample_type & PERF_SAMPLE_BUILD_ID_OFFSET) &&
+ (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) ||
+ event->attr.exclude_callchain_user);
+}
+
static void unaccount_event(struct perf_event *event)
{
bool dec = false;
@@ -5652,6 +5673,10 @@ static void unaccount_event(struct perf_event *event)
atomic_dec(&nr_mmap_events);
if (event->attr.build_id)
atomic_dec(&nr_build_id_events);
+ if (has_defer_output_build_id_offset(event))
+ atomic_dec(&nr_defer_output_build_id_events);
+ if (has_defer_output_callchain_build_id_offset(event))
+ atomic_dec(&nr_defer_output_callchain_build_id_events);
if (event->attr.comm)
atomic_dec(&nr_comm_events);
if (event->attr.namespaces)
@@ -8399,6 +8424,8 @@ static void perf_output_read(struct perf_output_handle *handle,
perf_output_read_one(handle, event, enabled, running);
}
+static struct unwind_work perf_unwind_work;
+
static void perf_bpf_to_sample_bid_offset(struct bpf_stack_build_id *bpf_bid)
{
struct perf_sample_build_id_offset *bid = (void *)bpf_bid;
@@ -8434,7 +8461,28 @@ static void perf_output_sample_build_id(struct perf_output_handle *handle,
PERF_RECORD_MISC_USER;
if (sample_bid) {
- if (is_user && has_user_ctx) {
+ u64 defer_cookie = 0;
+ bool defer_user = IS_ENABLED(CONFIG_UNWIND_USER) &&
+ is_user && has_user_ctx &&
+ event->attr.defer_callchain &&
+ (!(sample_type & PERF_SAMPLE_CALLCHAIN) ||
+ event->attr.exclude_callchain_user) &&
+ data->ip == instruction_pointer(task_pt_regs(current));
+
+ if (defer_user &&
+ unwind_deferred_request(&perf_unwind_work, &defer_cookie) < 0)
+ defer_cookie = 0;
+
+ if (defer_cookie) {
+ u64 deferred_bid[4] = {
+ 0,
+ PERF_CONTEXT_USER_DEFERRED,
+ defer_cookie,
+ data->ip,
+ };
+
+ perf_output_put(handle, deferred_bid);
+ } else if (is_user && has_user_ctx) {
if (callchain_bid && data->callchain_build_id &&
data->callchain_build_id->has_ip_bid) {
perf_output_put(handle,
@@ -8811,8 +8859,6 @@ static u64 perf_get_page_size(unsigned long addr)
static struct perf_callchain_entry __empty_callchain = { .nr = 0, };
-static struct unwind_work perf_unwind_work;
-
struct perf_callchain_entry *
perf_callchain(struct perf_event *event, struct pt_regs *regs)
{
@@ -10635,34 +10681,117 @@ void perf_event_bpf_event(struct bpf_prog *prog,
struct perf_callchain_deferred_event {
struct unwind_stacktrace *trace;
+ struct perf_callchain_build_id_entry *bid_entry;
struct {
struct perf_event_header header;
u64 cookie;
} event;
};
+static struct perf_callchain_build_id_entry *
+perf_resolve_build_id_offsets(struct unwind_stacktrace *trace)
+{
+ struct perf_callchain_build_id_entry *entry;
+ struct bpf_stack_build_id *bpf_bids;
+ u16 nr = trace->nr;
+ u32 max_size;
+
+ if (!nr || !current->mm ||
+ !atomic_read(&nr_defer_output_build_id_events))
+ return NULL;
+
+ if (!atomic_read(&nr_defer_output_callchain_build_id_events))
+ nr = 1;
+
+ entry = kzalloc(sizeof(*entry) + nr * sizeof(u64), GFP_KERNEL);
+ if (!entry)
+ return NULL;
+
+ bpf_bids = kcalloc(nr, sizeof(*bpf_bids), GFP_KERNEL);
+ if (!bpf_bids) {
+ kfree(entry);
+ return NULL;
+ }
+
+ for (u16 i = 0; i < nr; i++) {
+ entry->offsets[i] = trace->entries[i];
+ bpf_bids[i].ip = trace->entries[i];
+ }
+
+ stack_map_get_build_id_offset(bpf_bids, nr, true, true);
+
+ max_size = round_down(U16_MAX - sizeof(struct perf_event_header) -
+ 2 * sizeof(u64), sizeof(u64));
+ perf_callchain_encode_build_ids(entry, entry->offsets, nr, 0,
+ bpf_bids, nr, max_size);
+ kfree(bpf_bids);
+ return entry;
+}
+
static void perf_callchain_deferred_output(struct perf_event *event, void *data)
{
struct perf_callchain_deferred_event *deferred_event = data;
+ struct perf_callchain_build_id_entry *bid_entry = deferred_event->bid_entry;
struct perf_output_handle handle;
struct perf_sample_data sample;
u64 nr = deferred_event->trace->nr;
- u16 size, id_size, max_payload;
+ u32 max_offsets = 0, nr_build_ids = 0, nr_offsets = 0;
+ u32 raw_bids_len = 0, aligned_bids_len = 0;
+ bool use_bid;
+ u16 size, misc, id_size, max_payload;
int ret;
if (!event->attr.defer_output)
return;
+ use_bid = has_defer_output_build_id_offset(event);
+ if (use_bid && bid_entry) {
+ if (has_defer_output_callchain_build_id_offset(event))
+ max_offsets = bid_entry->nr_offsets;
+ else
+ max_offsets = min_t(u32, bid_entry->nr_offsets, 1);
+ }
+
id_size = event->attr.sample_id_all ? event->id_header_size : 0;
max_payload = round_down(U16_MAX - sizeof(deferred_event->event) -
sizeof(u64) - id_size, sizeof(u64));
- nr = min_t(u64, nr, max_payload / sizeof(u64));
- size = sizeof(deferred_event->event) + sizeof(u64) + nr * sizeof(u64);
+
+ if (use_bid) {
+ for (u32 i = 0; i < max_offsets; i++) {
+ u8 bid_idx = bid_entry->offsets[i] >> 56;
+ u32 next_nr_bids = nr_build_ids;
+ u32 next_raw_len = raw_bids_len;
+ u32 next_aligned_len;
+
+ if (bid_idx > 0 && bid_idx <= bid_entry->nr_build_ids &&
+ bid_idx > next_nr_bids) {
+ next_nr_bids = bid_idx;
+ next_raw_len = next_nr_bids *
+ (1 + BUILD_ID_SIZE_MAX);
+ }
+ next_aligned_len = ALIGN(next_raw_len, sizeof(u64));
+ if (next_aligned_len + (i + 1) * sizeof(u64) > max_payload)
+ break;
+ nr_build_ids = next_nr_bids;
+ raw_bids_len = next_raw_len;
+ aligned_bids_len = next_aligned_len;
+ nr_offsets = i + 1;
+ }
+ size = sizeof(deferred_event->event) + sizeof(u64) +
+ aligned_bids_len + nr_offsets * sizeof(u64);
+ } else {
+ nr = min_t(u64, nr, max_payload / sizeof(u64));
+ size = sizeof(deferred_event->event) + sizeof(u64) +
+ nr * sizeof(u64);
+ }
+
+ misc = PERF_RECORD_MISC_USER |
+ (use_bid ? PERF_RECORD_MISC_MMAP_BUILD_ID : 0);
/* XXX do we really need sample_id_all for this ??? */
perf_event_header__init(&deferred_event->event.header, &sample,
PERF_RECORD_CALLCHAIN_DEFERRED,
- PERF_RECORD_MISC_USER,
+ misc,
size,
event);
@@ -10672,11 +10801,27 @@ static void perf_callchain_deferred_output(struct perf_event *event, void *data)
return;
perf_output_put(&handle, deferred_event->event);
- perf_output_put(&handle, nr);
- for (u64 i = 0; i < nr; i++) {
- u64 entry = deferred_event->trace->entries[i];
+ if (use_bid) {
+ u64 pad_zero = 0;
+ u32 pad = aligned_bids_len - raw_bids_len;
+
+ perf_output_put(&handle, nr_build_ids);
+ perf_output_put(&handle, nr_offsets);
+ if (raw_bids_len)
+ __output_copy(&handle, bid_entry->build_ids, raw_bids_len);
+ if (pad)
+ __output_copy(&handle, &pad_zero, pad);
+ if (nr_offsets) {
+ __output_copy(&handle, bid_entry->offsets,
+ nr_offsets * sizeof(u64));
+ }
+ } else {
+ perf_output_put(&handle, nr);
+ for (u64 i = 0; i < nr; i++) {
+ u64 entry = deferred_event->trace->entries[i];
- perf_output_put(&handle, entry);
+ perf_output_put(&handle, entry);
+ }
}
perf_event__output_id_sample(event, &handle, &sample);
@@ -10686,14 +10831,18 @@ static void perf_callchain_deferred_output(struct perf_event *event, void *data)
static void perf_unwind_deferred_callback(struct unwind_work *work,
struct unwind_stacktrace *trace, u64 cookie)
{
+ struct perf_callchain_build_id_entry *bid_entry =
+ perf_resolve_build_id_offsets(trace);
struct perf_callchain_deferred_event deferred_event = {
.trace = trace,
+ .bid_entry = bid_entry,
.event = {
.cookie = cookie,
},
};
perf_iterate_sb(perf_callchain_deferred_output, &deferred_event, NULL);
+ kfree(bid_entry);
}
struct perf_text_poke_event {
@@ -13552,6 +13701,10 @@ static void account_event(struct perf_event *event)
atomic_inc(&nr_mmap_events);
if (event->attr.build_id)
atomic_inc(&nr_build_id_events);
+ if (has_defer_output_build_id_offset(event))
+ atomic_inc(&nr_defer_output_build_id_events);
+ if (has_defer_output_callchain_build_id_offset(event))
+ atomic_inc(&nr_defer_output_callchain_build_id_events);
if (event->attr.comm)
atomic_inc(&nr_comm_events);
if (event->attr.namespaces)
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 07/19] perf inject: Set ordered_events and ordering_requires_timestamps for --aslr
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (5 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 06/19] perf/core: Support deferred unwinding for BUILD_ID_OFFSET " Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 08/19] perf session: Avoid double byte-swapping re-parsed deferred samples Ian Rogers
` (11 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Commit 0ce6cf53db5b ("perf inject: Set ordering_requires_timestamps
before creating session") initialized ordered_events and
ordering_requires_timestamps on inject.tool before creating delegate
tools and the session, but omitted inject.aslr. Also set
ordering_requires_timestamps on aslr->tool.tool alongside ordered_events
in aslr_tool__init() so --aslr orders MMAP and sample events by
timestamp consistently.
Fixes: febea9ec382f ("perf inject/aslr: Add ASLR tool infrastructure and MMAP tracking")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-inject.c | 4 ++--
tools/perf/util/aslr.c | 1 +
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 3f3108fd3981..b887a4398f96 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -2810,7 +2810,7 @@ int cmd_inject(int argc, const char **argv)
data.path = inject.input_name;
- ordered_events = inject.jit_mode || inject.sched_stat ||
+ ordered_events = inject.jit_mode || inject.sched_stat || inject.aslr ||
inject.itrace_synth_opts.vm_time_correlation ||
inject.itrace_synth_opts.set ||
inject.guest_session.perf_data_file ||
@@ -2861,7 +2861,7 @@ int cmd_inject(int argc, const char **argv)
inject.tool.schedstat_domain = perf_event__repipe_op2_synth;
inject.tool.dont_split_sample_group = true;
inject.tool.merge_deferred_callchains = false;
- if (inject.jit_mode ||
+ if (inject.jit_mode || inject.aslr ||
inject.itrace_synth_opts.vm_time_correlation ||
inject.itrace_synth_opts.set ||
inject.guest_session.perf_data_file ||
diff --git a/tools/perf/util/aslr.c b/tools/perf/util/aslr.c
index e365f807b021..dee077cca5fe 100644
--- a/tools/perf/util/aslr.c
+++ b/tools/perf/util/aslr.c
@@ -1273,6 +1273,7 @@ static int aslr_tool__init(struct aslr_tool *aslr, struct perf_tool *delegate)
{
delegate_tool__init(&aslr->tool, delegate);
aslr->tool.tool.ordered_events = true;
+ aslr->tool.tool.ordering_requires_timestamps = true;
if (machines__init(&aslr->machines, /*session=*/NULL))
return -ENOMEM;
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 08/19] perf session: Avoid double byte-swapping re-parsed deferred samples
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (6 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 07/19] perf inject: Set ordered_events and ordering_requires_timestamps for --aslr Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 09/19] perf inject: Fix PERF_SAMPLE_READ layout and bounds checks in aslr_tool__process_sample() Ian Rogers
` (10 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
When perf_session__deliver_event() processes a cross-endian sample,
__evsel__parse_sample() is invoked with needs_swap=true and byte-swaps
payload regions such as PERF_SAMPLE_RAW, PERF_SAMPLE_BRANCH_STACK, and
PERF_SAMPLE_AUX in-place in the event buffer.
If the sample has a deferred callchain and merge_deferred_callchains is
enabled, machines__deliver_event() copies that already-parsed event into
de->event. Later, evlist__deliver_deferred_callchain() (potentially on
multiple iterations before finding a matching tid) and
session__flush_deferred_samples() re-parse de->event via
evlist__parse_sample(), which runs __evsel__parse_sample() again with
evsel->needs_swap still true, double-swapping in-place fields. Meanwhile,
pyrf_event__copy() re-parses copied events with needs_swap=false, which
misses PERF_SAMPLE_TID, PERF_SAMPLE_CPU, and PERF_SAMPLE_RAW's size word
because they were only unswapped into local variables.
Finish unswapping PERF_SAMPLE_TID, PERF_SAMPLE_CPU, and PERF_SAMPLE_RAW's
size prefix in-place during the initial cross-endian parse so the entire
sample buffer is in host layout afterwards, filter deferred samples by
stored tid before re-parsing, and re-parse de->event via
evlist__parse_sample_unswapped().
Fixes: 9b4525fd089d ("perf tools: Merge deferred user callchains")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/evlist.c | 38 +++++++++++++++++++++++++++++---------
tools/perf/util/evlist.h | 2 ++
tools/perf/util/evsel.c | 10 +++++++++-
tools/perf/util/session.c | 14 +++++++-------
4 files changed, 47 insertions(+), 17 deletions(-)
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index c2402e4791b6..ec665828d4e3 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1935,17 +1935,12 @@ void evlist__cancel_workload(struct evlist *evlist)
}
}
-int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
+static int __evlist__parse_sample(struct evlist *evlist, struct evsel *evsel,
+ union perf_event *event,
+ struct perf_sample *sample, bool needs_swap)
{
- struct evsel *evsel = evlist__event2evsel(evlist, event);
- int ret;
+ int ret = __evsel__parse_sample(evsel, event, sample, needs_swap);
- if (!evsel) {
- /* Ensure the sample is okay for perf_sample__exit. */
- perf_sample__init(sample, /*all=*/false);
- return -EFAULT;
- }
- ret = evsel__parse_sample(evsel, event, sample);
if (ret)
return ret;
if (perf_guest && sample->id) {
@@ -1959,6 +1954,31 @@ int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct
return 0;
}
+int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
+{
+ struct evsel *evsel = evlist__event2evsel(evlist, event);
+
+ if (!evsel) {
+ /* Ensure the sample is okay for perf_sample__exit. */
+ perf_sample__init(sample, /*all=*/false);
+ return -EFAULT;
+ }
+ return __evlist__parse_sample(evlist, evsel, event, sample, evsel->needs_swap);
+}
+
+int evlist__parse_sample_unswapped(struct evlist *evlist, union perf_event *event,
+ struct perf_sample *sample)
+{
+ struct evsel *evsel = evlist__event2evsel(evlist, event);
+
+ if (!evsel) {
+ /* Ensure the sample is okay for perf_sample__exit. */
+ perf_sample__init(sample, /*all=*/false);
+ return -EFAULT;
+ }
+ return __evlist__parse_sample(evlist, evsel, event, sample, /*needs_swap=*/false);
+}
+
int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
{
struct evsel *evsel = evlist__event2evsel(evlist, event);
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 0f380421e03d..e42d590d4732 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -450,6 +450,8 @@ bool evlist__sample_id_all(struct evlist *evlist);
u16 evlist__id_hdr_size(struct evlist *evlist);
int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample);
+int evlist__parse_sample_unswapped(struct evlist *evlist, union perf_event *event,
+ struct perf_sample *sample);
int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp);
bool evlist__valid_sample_type(struct evlist *evlist);
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 737a8680aaae..322582bb4f40 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -3520,6 +3520,7 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,
u.val64 = bswap_64(u.val64);
u.val32[0] = bswap_32(u.val32[0]);
u.val32[1] = bswap_32(u.val32[1]);
+ *(__u64 *)array = u.val64;
}
data->pid = u.val32[0];
@@ -3554,6 +3555,8 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,
/* undo swap of u64, then swap on individual u32s */
u.val64 = bswap_64(u.val64);
u.val32[0] = bswap_32(u.val32[0]);
+ u.val32[1] = bswap_32(u.val32[1]);
+ *(__u64 *)array = u.val64;
}
data->cpu = u.val32[0];
@@ -3665,7 +3668,12 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,
/* mem_bswap_64() accesses complete 64-bit words. */
sz = roundup((u64)data->raw_size, sizeof(u64));
OVERFLOW_CHECK(raw, sz, max_size);
- mem_bswap_64((void *)raw, data->raw_size);
+ if (data->raw_size) {
+ mem_bswap_64((void *)raw, data->raw_size);
+ *(u32 *)raw = data->raw_size;
+ } else {
+ *(__u64 *)raw = u.val64;
+ }
}
data->raw_data = (void *)array;
array = (void *)array + data->raw_size;
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index a58b4fe44b62..ec1ea510ae61 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1933,6 +1933,7 @@ struct deferred_event {
struct list_head list;
union perf_event *event;
u64 file_offset;
+ u32 tid;
};
/*
@@ -1964,8 +1965,11 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,
struct perf_sample orig_sample;
struct evsel *new_evsel;
+ if (sample->tid != de->tid)
+ continue;
+
perf_sample__init(&orig_sample, /*all=*/false);
- ret = evlist__parse_sample(evlist, de->event, &orig_sample);
+ ret = evlist__parse_sample_unswapped(evlist, de->event, &orig_sample);
if (ret < 0) {
pr_err("failed to parse original sample\n");
perf_sample__exit(&orig_sample);
@@ -1973,11 +1977,6 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,
}
orig_sample.file_offset = de->file_offset;
- if (sample->tid != orig_sample.tid) {
- perf_sample__exit(&orig_sample);
- continue;
- }
-
if (event->callchain_deferred.cookie == orig_sample.deferred_cookie)
sample__merge_deferred_callchain(&orig_sample, sample);
else
@@ -2027,7 +2026,7 @@ static int session__flush_deferred_samples(struct perf_session *session,
}
perf_sample__init(&sample, /*all=*/false);
- ret = evlist__parse_sample(evlist, de->event, &sample);
+ ret = evlist__parse_sample_unswapped(evlist, de->event, &sample);
if (ret < 0) {
pr_err("failed to parse original sample\n");
perf_sample__exit(&sample);
@@ -2120,6 +2119,7 @@ static int machines__deliver_event(struct machines *machines,
}
memcpy(de->event, event, sz);
de->file_offset = sample->file_offset;
+ de->tid = sample->tid;
list_add_tail(&de->list, evlist__deferred_samples(evlist));
return 0;
}
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 09/19] perf inject: Fix PERF_SAMPLE_READ layout and bounds checks in aslr_tool__process_sample()
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (7 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 08/19] perf session: Avoid double byte-swapping re-parsed deferred samples Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 10/19] tools headers UAPI: Sync linux/perf_event.h for struct perf_build_id Ian Rogers
` (9 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
In aslr_tool__process_sample(), PERF_SAMPLE_READ only copied the id word
when evsel->core.attr.read_format had PERF_FORMAT_ID set, whereas the
kernel and __evsel__parse_sample() unconditionally include the id word in
both single-counter and PERF_FORMAT_GROUP modes. When PERF_FORMAT_ID was
not explicitly set in attr.read_format, in_array[i] became desynchronized
with subsequent sample fields.
In addition, CHECK_BOUNDS() used addition (i + required_i > max_i) which
could overflow, and the dropped PERF_SAMPLE_RAW, PERF_SAMPLE_STACK_USER,
and PERF_SAMPLE_AUX handlers copied word-rounded lengths into out_array
before discarding the sample.
Unconditionally copy the id word in PERF_SAMPLE_READ using
sample_read_value_size() with division-based bounds checks, make
CHECK_BOUNDS() subtraction-based, and drop RAW/STACK_USER/AUX samples
directly without dead copies.
Fixes: 575c6d2bc470 ("perf inject/aslr: Implement sample address remapping")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/aslr.c | 95 +++++++++---------------------------------
1 file changed, 20 insertions(+), 75 deletions(-)
diff --git a/tools/perf/util/aslr.c b/tools/perf/util/aslr.c
index dee077cca5fe..e236d5287043 100644
--- a/tools/perf/util/aslr.c
+++ b/tools/perf/util/aslr.c
@@ -8,6 +8,7 @@
#include "evlist.h"
#include "machine.h"
#include "map.h"
+#include "sample.h"
#include "thread.h"
#include "tool.h"
#include "session.h"
@@ -780,7 +781,7 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,
out_array = &new_event->sample.array[0];
#define CHECK_BOUNDS(required_i, required_j) \
- (i + (required_i) > max_i || j + (required_j) > max_j)
+ ((required_i) > max_i - i || (required_j) > max_j - j)
#define COPY_U64() \
do { \
@@ -835,18 +836,20 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,
if (orig_sample_type & PERF_SAMPLE_PERIOD)
COPY_U64(); /* period */
if (orig_sample_type & PERF_SAMPLE_READ) {
- if ((evsel->core.attr.read_format & PERF_FORMAT_GROUP) == 0) {
+ u64 read_format = evsel->core.attr.read_format;
+
+ if ((read_format & PERF_FORMAT_GROUP) == 0) {
COPY_U64(); /* value */
- if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
+ if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
COPY_U64(); /* time_enabled */
- if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
+ if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
COPY_U64(); /* time_running */
- if (evsel->core.attr.read_format & PERF_FORMAT_ID)
- COPY_U64(); /* id */
- if (evsel->core.attr.read_format & PERF_FORMAT_LOST)
+ /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
+ COPY_U64(); /* id */
+ if (read_format & PERF_FORMAT_LOST)
COPY_U64(); /* lost */
} else {
- u64 nr;
+ u64 nr, entry_words;
if (CHECK_BOUNDS(1, 1)) {
ret = -EFAULT;
@@ -854,17 +857,18 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,
}
nr = in_array[i];
COPY_U64();
- if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
+ if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
COPY_U64(); /* time_enabled */
- if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
+ if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
COPY_U64(); /* time_running */
- for (u64 cntr = 0; cntr < nr; cntr++) {
- COPY_U64(); /* value */
- if (evsel->core.attr.read_format & PERF_FORMAT_ID)
- COPY_U64(); /* id */
- if (evsel->core.attr.read_format & PERF_FORMAT_LOST)
- COPY_U64(); /* lost */
+ entry_words = sample_read_value_size(read_format) / sizeof(u64);
+ if (nr > (max_i - i) / entry_words ||
+ nr > (max_j - j) / entry_words) {
+ ret = -EFAULT;
+ goto out_put;
}
+ for (u64 cntr = 0; cntr < nr * entry_words; cntr++)
+ out_array[j++] = in_array[i++];
}
}
if (orig_sample_type & PERF_SAMPLE_CALLCHAIN) {
@@ -934,16 +938,6 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,
}
}
if (orig_sample_type & PERF_SAMPLE_RAW) {
- size_t bytes = sizeof(u32) + sample->raw_size;
- size_t u64_words = (bytes + 7) / 8;
-
- if (i + u64_words > max_i || j + u64_words > max_j) {
- ret = -EFAULT;
- goto out_put;
- }
- memcpy(&out_array[j], &in_array[i], bytes);
- i += u64_words;
- j += u64_words;
/*
* TODO: certain raw samples can be remapped, such as
* tracepoints by examining their fields.
@@ -1041,30 +1035,6 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,
}
}
if (orig_sample_type & PERF_SAMPLE_STACK_USER) {
- u64 size;
-
- if (CHECK_BOUNDS(1, 1)) {
- ret = -EFAULT;
- goto out_put;
- }
- size = in_array[i];
- COPY_U64();
- if (size > 0) {
- size_t u64_words = size / 8 + (size % 8 ? 1 : 0);
-
- if (u64_words > max_i - i || u64_words > max_j - j) {
- ret = -EFAULT;
- goto out_put;
- }
- memcpy(&out_array[j], &in_array[i], size);
- if (size % 8) {
- size_t pad = 8 - (size % 8);
-
- memset(((char *)&out_array[j]) + size, 0, pad);
- }
- i += u64_words;
- j += u64_words;
- }
/* TODO: can this be less conservative? */
pr_debug("Dropping stack user sample as possible ASLR leak\n");
ret = 0;
@@ -1110,31 +1080,6 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,
COPY_U64(); /* code_page_size */
if (orig_sample_type & PERF_SAMPLE_AUX) {
- u64 size;
-
- if (CHECK_BOUNDS(1, 1)) {
- ret = -EFAULT;
- goto out_put;
- }
- out_array[j] = in_array[i];
- size = out_array[j++];
- i++;
- if (size > 0) {
- size_t u64_words = size / 8 + (size % 8 ? 1 : 0);
-
- if (u64_words > max_i - i || u64_words > max_j - j) {
- ret = -EFAULT;
- goto out_put;
- }
- memcpy(&out_array[j], &in_array[i], size);
- if (size % 8) {
- size_t pad = 8 - (size % 8);
-
- memset(((char *)&out_array[j]) + size, 0, pad);
- }
- i += u64_words;
- j += u64_words;
- }
/* TODO: can this be less conservative? */
pr_debug("Dropping aux sample as possible ASLR leak\n");
ret = 0;
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 10/19] tools headers UAPI: Sync linux/perf_event.h for struct perf_build_id
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (8 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 09/19] perf inject: Fix PERF_SAMPLE_READ layout and bounds checks in aslr_tool__process_sample() Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 11/19] tools headers UAPI: Sync linux/perf_event.h for BUILD_ID_OFFSET Ian Rogers
` (8 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Synchronize tools/include/uapi/linux/perf_event.h, define struct
perf_build_id in tools/lib/perf/include/perf/event.h (keeping the
existing anonymous build_id_size/build_id fields in struct
perf_record_mmap2 for source compatibility), and update perf tools to
use struct perf_build_id.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/include/uapi/linux/perf_event.h | 13 ++++++++++
tools/lib/perf/include/perf/event.h | 14 +++++++++--
tools/perf/builtin-inject.c | 3 ++-
tools/perf/util/event.c | 4 +--
tools/perf/util/machine.c | 3 ++-
tools/perf/util/python.c | 8 +++---
tools/perf/util/synthetic-events.c | 36 ++++++++++++++-------------
7 files changed, 54 insertions(+), 27 deletions(-)
diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h
index c49fc76292f7..deb7c42d109b 100644
--- a/tools/include/uapi/linux/perf_event.h
+++ b/tools/include/uapi/linux/perf_event.h
@@ -1099,6 +1099,18 @@ enum perf_event_type {
*/
PERF_RECORD_SAMPLE = 9,
+ /*
+ * Build IDs may be present in a number of events. They have a
+ * consistent encoding of:
+ *
+ * struct perf_build_id {
+ * u8 size;
+ * u8 __reserved_1;
+ * u16 __reserved_2;
+ * u8 data[20];
+ * };
+ */
+
/*
* The MMAP2 records are an augmented version of MMAP, they add
* maj, min, ino numbers to be used to uniquely identify each mapping
@@ -1123,6 +1135,7 @@ enum perf_event_type {
* u16 __reserved_2;
* u8 build_id[20];
* };
+ * struct perf_build_id bid;
* };
* u32 prot, flags;
* char filename[];
diff --git a/tools/lib/perf/include/perf/event.h b/tools/lib/perf/include/perf/event.h
index fdced574c889..867dd55f0969 100644
--- a/tools/lib/perf/include/perf/event.h
+++ b/tools/lib/perf/include/perf/event.h
@@ -26,6 +26,15 @@ struct perf_record_mmap {
char filename[PATH_MAX];
};
+#define PERF_BUILD_ID_SIZE 20
+
+struct perf_build_id {
+ __u8 size;
+ __u8 __reserved_1;
+ __u16 __reserved_2;
+ __u8 data[PERF_BUILD_ID_SIZE];
+};
+
struct perf_record_mmap2 {
struct perf_event_header header;
__u32 pid, tid;
@@ -43,8 +52,9 @@ struct perf_record_mmap2 {
__u8 build_id_size;
__u8 __reserved_1;
__u16 __reserved_2;
- __u8 build_id[20];
+ __u8 build_id[PERF_BUILD_ID_SIZE];
};
+ struct perf_build_id bid;
};
__u32 prot;
__u32 flags;
@@ -321,7 +331,7 @@ struct perf_record_header_build_id {
union {
__u8 build_id[24];
struct {
- __u8 data[20];
+ __u8 data[PERF_BUILD_ID_SIZE];
__u8 size;
__u8 reserved1__;
__u16 reserved2__;
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index b887a4398f96..a278c360b058 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -811,7 +811,8 @@ static int perf_event__repipe_mmap2(const struct perf_tool *tool,
struct dso_id id = dso_id_empty;
if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
- build_id__init(&id.build_id, event->mmap2.build_id, event->mmap2.build_id_size);
+ build_id__init(&id.build_id, event->mmap2.bid.data,
+ event->mmap2.bid.size);
} else {
id.maj = event->mmap2.maj;
id.min = event->mmap2.min;
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index 2f80e364659f..cefb32833da8 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -335,8 +335,8 @@ size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
char sbuild_id[SBUILD_ID_SIZE];
struct build_id bid;
- build_id__init(&bid, event->mmap2.build_id,
- event->mmap2.build_id_size);
+ build_id__init(&bid, event->mmap2.bid.data,
+ event->mmap2.bid.size);
build_id__snprintf(&bid, sbuild_id, sizeof(sbuild_id));
return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 5b4550e97172..c7f5ef32d5bd 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -1828,7 +1828,8 @@ int machine__process_mmap2_event(struct machine *machine,
perf_event__fprintf_mmap2(event, stdout);
if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
- build_id__init(&dso_id.build_id, event->mmap2.build_id, event->mmap2.build_id_size);
+ build_id__init(&dso_id.build_id, event->mmap2.bid.data,
+ event->mmap2.bid.size);
} else {
dso_id.maj = event->mmap2.maj;
dso_id.min = event->mmap2.min;
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 902157c472b3..1efbe5bb26b1 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -302,12 +302,12 @@ static PyObject *pyrf_mmap2_event__get_build_id(PyObject *self, void *closure __
if (!(pevent->event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID))
Py_RETURN_NONE;
- int size = pevent->event->mmap2.build_id_size;
+ size_t size = pevent->event->mmap2.bid.size;
- if (size > 20)
- size = 20;
+ if (size > sizeof(pevent->event->mmap2.bid.data))
+ size = sizeof(pevent->event->mmap2.bid.data);
- return PyBytes_FromStringAndSize((const char *)pevent->event->mmap2.build_id, size);
+ return PyBytes_FromStringAndSize((const char *)pevent->event->mmap2.bid.data, size);
}
static PyGetSetDef pyrf_mmap2_event__getset[] = {
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index 3c8d29f27fd9..57d6efe9e5f5 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -452,7 +452,7 @@ static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event,
}
if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
- build_id__init(&dso_id.build_id, event->build_id, event->build_id_size);
+ build_id__init(&dso_id.build_id, event->bid.data, event->bid.size);
} else {
dso_id.maj = event->maj;
dso_id.min = event->min;
@@ -479,11 +479,11 @@ static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event,
out:
if (rc == 0) {
- memcpy(event->build_id, bid.data, sizeof(bid.data));
- event->build_id_size = (u8) bid.size;
+ memcpy(event->bid.data, bid.data, sizeof(bid.data));
+ event->bid.size = (u8) bid.size;
event->header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID;
- event->__reserved_1 = 0;
- event->__reserved_2 = 0;
+ event->bid.__reserved_1 = 0;
+ event->bid.__reserved_2 = 0;
if (dso && !dso__has_build_id(dso))
dso__set_build_id(dso, &bid);
@@ -611,8 +611,10 @@ int perf_event__synthesize_mmap_events(const struct perf_tool *tool,
event->mmap2.prot = prot;
event->mmap2.flags = flags;
- if (!symbol_conf.no_buildid_mmap2)
- perf_record_mmap2__read_build_id(&event->mmap2, machine, false);
+ if (!symbol_conf.no_buildid_mmap2) {
+ perf_record_mmap2__read_build_id(&event->mmap2, machine,
+ /*is_kernel=*/false);
+ }
if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
rc = -1;
@@ -807,12 +809,12 @@ static int perf_event__synthesize_modules_maps_cb(struct map *map, void *data)
/* Clear stale build ID and entire union from previous module iteration */
event->mmap2.header.misc &= ~PERF_RECORD_MISC_MMAP_BUILD_ID;
- memset(event->mmap2.build_id, 0, sizeof(event->mmap2.build_id));
- event->mmap2.build_id_size = 0;
- event->mmap2.__reserved_1 = 0;
- event->mmap2.__reserved_2 = 0;
+ memset(event->mmap2.bid.data, 0, sizeof(event->mmap2.bid.data));
+ event->mmap2.bid.size = 0;
+ event->mmap2.bid.__reserved_1 = 0;
+ event->mmap2.bid.__reserved_2 = 0;
- perf_record_mmap2__read_build_id(&event->mmap2, args->machine, false);
+ perf_record_mmap2__read_build_id(&event->mmap2, args->machine, /*is_kernel=*/false);
} else {
const char *long_name = dso__long_name(dso);
@@ -1299,7 +1301,7 @@ static int __perf_event__synthesize_kernel_mmap(const struct perf_tool *tool,
event->mmap2.len = map__end(map) - event->mmap.start;
event->mmap2.pid = machine->pid;
- perf_record_mmap2__read_build_id(&event->mmap2, machine, true);
+ perf_record_mmap2__read_build_id(&event->mmap2, machine, /*is_kernel=*/true);
} else {
size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
"%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
@@ -2530,10 +2532,10 @@ int perf_event__synthesize_mmap2_build_id(const struct perf_tool *tool,
ev.mmap2.len = len;
ev.mmap2.pgoff = pgoff;
- ev.mmap2.build_id_size = bid->size;
- if (ev.mmap2.build_id_size > sizeof(ev.mmap2.build_id))
- ev.mmap2.build_id_size = sizeof(ev.mmap2.build_id);
- memcpy(ev.mmap2.build_id, bid->data, ev.mmap2.build_id_size);
+ ev.mmap2.bid.size = bid->size;
+ if (ev.mmap2.bid.size > sizeof(ev.mmap2.bid.data))
+ ev.mmap2.bid.size = sizeof(ev.mmap2.bid.data);
+ memcpy(ev.mmap2.bid.data, bid->data, ev.mmap2.bid.size);
ev.mmap2.prot = prot;
ev.mmap2.flags = flags;
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 11/19] tools headers UAPI: Sync linux/perf_event.h for BUILD_ID_OFFSET
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (9 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 10/19] tools headers UAPI: Sync linux/perf_event.h for struct perf_build_id Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 12/19] perf: Refactor thread map and symbol APIs to take perf_sample Ian Rogers
` (7 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Synchronize tools/include/uapi/linux/perf_event.h with the kernel UAPI
header for PERF_SAMPLE_BUILD_ID_OFFSET and
PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET, and add struct
perf_sample_build_id_offset and the struct perf_record_callchain_deferred
build ID union fields to tools/lib/perf/include/perf/event.h.
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/include/uapi/linux/perf_event.h | 59 ++++++++++++++++++++++++---
tools/lib/perf/include/perf/event.h | 29 ++++++++++++-
2 files changed, 81 insertions(+), 7 deletions(-)
diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h
index deb7c42d109b..775420ed9aee 100644
--- a/tools/include/uapi/linux/perf_event.h
+++ b/tools/include/uapi/linux/perf_event.h
@@ -165,8 +165,10 @@ enum perf_event_sample_format {
PERF_SAMPLE_DATA_PAGE_SIZE = 1U << 22,
PERF_SAMPLE_CODE_PAGE_SIZE = 1U << 23,
PERF_SAMPLE_WEIGHT_STRUCT = 1U << 24,
+ PERF_SAMPLE_BUILD_ID_OFFSET = 1U << 25,
+ PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET = 1U << 26,
- PERF_SAMPLE_MAX = 1U << 25, /* non-ABI */
+ PERF_SAMPLE_MAX = 1U << 27, /* non-ABI */
};
#define PERF_SAMPLE_WEIGHT_TYPE (PERF_SAMPLE_WEIGHT | PERF_SAMPLE_WEIGHT_STRUCT)
@@ -840,7 +842,8 @@ struct perf_event_mmap_page {
*
* PERF_RECORD_MISC_EXACT_IP - PERF_RECORD_SAMPLE of precise events
* PERF_RECORD_MISC_SWITCH_OUT_PREEMPT - PERF_RECORD_SWITCH* events
- * PERF_RECORD_MISC_MMAP_BUILD_ID - PERF_RECORD_MMAP2 event
+ * PERF_RECORD_MISC_MMAP_BUILD_ID - PERF_RECORD_MMAP2 and
+ * PERF_RECORD_CALLCHAIN_DEFERRED events
*
*
* PERF_RECORD_MISC_EXACT_IP:
@@ -852,7 +855,7 @@ struct perf_event_mmap_page {
* Indicates that thread was preempted in TASK_RUNNING state.
*
* PERF_RECORD_MISC_MMAP_BUILD_ID:
- * Indicates that mmap2 event carries build ID data.
+ * Indicates that mmap2 or deferred callchain event carries build ID data.
*/
#define PERF_RECORD_MISC_EXACT_IP (1 << 14)
#define PERF_RECORD_MISC_SWITCH_OUT_PREEMPT (1 << 14)
@@ -1095,6 +1098,38 @@ enum perf_event_type {
* { u64 code_page_size;} && PERF_SAMPLE_CODE_PAGE_SIZE
* { u64 size;
* char data[size]; } && PERF_SAMPLE_AUX
+ * { union {
+ * struct {
+ * struct perf_build_id bid;
+ * u64 offset;
+ * };
+ * struct {
+ * u64 zero;
+ * u64 context;
+ * u64 cookie;
+ * u64 ip;
+ * }; && attr.defer_callchain
+ * }; } && PERF_SAMPLE_BUILD_ID_OFFSET
+ * { u32 nr_build_ids;
+ * u32 nr_offsets;
+ * struct {
+ * u8 size;
+ * u8 data[size];
+ * }[nr_build_ids];
+ * u8 pad[];
+ * u64 offsets[nr_offsets];
+ * #
+ * # Entries in offsets[] include PERF_CONTEXT_* markers (which
+ * # have top 8 bits 0xff). Kernel/hypervisor entries (after
+ * # PERF_CONTEXT_KERNEL, PERF_CONTEXT_HV, PERF_CONTEXT_GUEST,
+ * # or PERF_CONTEXT_GUEST_KERNEL) and the cookie word following
+ * # PERF_CONTEXT_USER_DEFERRED are raw 64-bit values. User
+ * # entries (after PERF_CONTEXT_USER or PERF_CONTEXT_GUEST_USER,
+ * # or in PERF_RECORD_CALLCHAIN_DEFERRED) use the top 8 bits to
+ * # encode a 1-based index (1..254) into the build_ids table and
+ * # the lower 56 bits for the DSO file offset, or 0x00 for an
+ * # unresolved user virtual address.
+ * } && PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET
* };
*/
PERF_RECORD_SAMPLE = 9,
@@ -1306,8 +1341,22 @@ enum perf_event_type {
* struct {
* struct perf_event_header header;
* u64 cookie;
- * u64 nr;
- * u64 ips[nr];
+ * union {
+ * struct {
+ * u64 nr;
+ * u64 ips[nr];
+ * };
+ * struct {
+ * u32 nr_build_ids;
+ * u32 nr_offsets;
+ * struct {
+ * u8 size;
+ * u8 data[size];
+ * }[nr_build_ids];
+ * u8 pad[];
+ * u64 offsets[nr_offsets];
+ * }; && (header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID)
+ * };
* struct sample_id sample_id;
* };
*/
diff --git a/tools/lib/perf/include/perf/event.h b/tools/lib/perf/include/perf/event.h
index 867dd55f0969..216f42f7485b 100644
--- a/tools/lib/perf/include/perf/event.h
+++ b/tools/lib/perf/include/perf/event.h
@@ -3,6 +3,7 @@
#define __LIBPERF_EVENT_H
#include <linux/perf_event.h>
+#include <linux/stddef.h>
#include <linux/types.h>
#include <linux/limits.h>
#include <linux/bpf.h>
@@ -168,6 +169,21 @@ struct perf_record_switch {
__u32 next_prev_tid;
};
+struct perf_sample_build_id_offset {
+ union {
+ struct perf_build_id bid;
+ struct {
+ __u64 zero;
+ __u64 context;
+ __u64 cookie;
+ };
+ };
+ union {
+ __u64 offset;
+ __u64 ip;
+ };
+};
+
struct perf_record_callchain_deferred {
struct perf_event_header header;
/*
@@ -176,8 +192,17 @@ struct perf_record_callchain_deferred {
* the PERF_CONTEXT_USER_DEFERRED entry.
*/
__u64 cookie;
- __u64 nr;
- __u64 ips[];
+ union {
+ struct {
+ __u64 nr;
+ __u64 ips[];
+ };
+ struct {
+ __u32 nr_build_ids;
+ __u32 nr_offsets;
+ __u8 build_ids[];
+ };
+ };
};
struct perf_record_header_attr {
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 12/19] perf: Refactor thread map and symbol APIs to take perf_sample
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (10 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 11/19] tools headers UAPI: Sync linux/perf_event.h for BUILD_ID_OFFSET Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 13/19] perf maps: Add dsos__find_by_build_id() and maps__find_by_build_id() Ian Rogers
` (6 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Refactor thread__find_map and thread__find_symbol family of APIs to
take a struct perf_sample * instead of cpumode and addr. This prepares
for looking up symbols using build ID and offset, which is contained in
the sample payload instead of the address.
Signed-off-by: Ian Rogers <irogers@google.com>
---
.../arch/powerpc/util/skip-callchain-idx.c | 4 +-
tools/perf/builtin-inject.c | 2 +-
tools/perf/builtin-script.c | 48 +++++++++++++----
tools/perf/builtin-timechart.c | 5 +-
tools/perf/builtin-trace.c | 10 ++--
tools/perf/tests/code-reading.c | 6 ++-
tools/perf/tests/mmap-thread-lookup.c | 9 +++-
tools/perf/util/annotate-data.c | 5 +-
tools/perf/util/aslr.c | 46 +++++++++++------
tools/perf/util/build-id.c | 2 +-
tools/perf/util/capstone.c | 4 +-
tools/perf/util/cs-etm.c | 10 +++-
tools/perf/util/data-convert-json.c | 4 +-
tools/perf/util/debug.c | 6 ++-
tools/perf/util/dlfilter.c | 8 ++-
tools/perf/util/event.c | 51 ++++++++++---------
tools/perf/util/intel-pt.c | 24 +++++++--
tools/perf/util/machine.c | 18 +++++--
tools/perf/util/python.c | 18 +++++--
tools/perf/util/thread.c | 11 ++--
tools/perf/util/thread.h | 14 ++---
tools/perf/util/unwind-libdw.c | 7 ++-
tools/perf/util/unwind-libunwind.c | 8 ++-
23 files changed, 224 insertions(+), 96 deletions(-)
diff --git a/tools/perf/arch/powerpc/util/skip-callchain-idx.c b/tools/perf/arch/powerpc/util/skip-callchain-idx.c
index 472714cfad38..cb29d9eafa89 100644
--- a/tools/perf/arch/powerpc/util/skip-callchain-idx.c
+++ b/tools/perf/arch/powerpc/util/skip-callchain-idx.c
@@ -225,7 +225,9 @@ int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
addr_location__init(&al);
ip = chain->ips[1];
- thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al);
+ thread__find_symbol(thread,
+ &(struct perf_sample){.cpumode = PERF_RECORD_MISC_USER, .ip = ip},
+ &al);
if (al.map)
dso = map__dso(al.map);
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index a278c360b058..f59f3005899c 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -1167,7 +1167,7 @@ static int perf_event__inject_buildid(const struct perf_tool *tool, union perf_e
goto repipe;
}
- if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) {
+ if (thread__find_map(thread, sample, &al)) {
mark_dso_hit(inject, tool, sample, machine, args.mmap_evsel, al.map,
/*sample_in_dso=*/true);
}
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index b6691ebb1b4e..376a4e0d7b0c 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -1063,6 +1063,7 @@ static int perf_sample__fprintf_brstack(struct perf_sample *sample,
{
struct branch_stack *br = sample->branch_stack;
struct branch_entry *entries = perf_sample__branch_entries(sample);
+ struct perf_sample br_sample = {};
u64 i, from, to;
int printed = 0;
@@ -1079,8 +1080,12 @@ static int perf_sample__fprintf_brstack(struct perf_sample *sample,
addr_location__init(&alf);
addr_location__init(&alt);
- thread__find_map_fb(thread, sample->cpumode, from, &alf);
- thread__find_map_fb(thread, sample->cpumode, to, &alt);
+ br_sample.cpumode = sample->cpumode;
+ br_sample.ip = from;
+ thread__find_map_fb(thread, &br_sample, &alf);
+ br_sample.cpumode = sample->cpumode;
+ br_sample.ip = to;
+ thread__find_map_fb(thread, &br_sample, &alt);
printed += map__fprintf_dsoname_dsoff(alf.map, PRINT_FIELD(DSOFF), alf.addr, fp);
printed += fprintf(fp, "/0x%"PRIx64, to);
@@ -1102,6 +1107,7 @@ static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
{
struct branch_stack *br = sample->branch_stack;
struct branch_entry *entries = perf_sample__branch_entries(sample);
+ struct perf_sample br_sample = {};
u64 i, from, to;
int printed = 0;
@@ -1116,8 +1122,12 @@ static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
from = entries[i].from;
to = entries[i].to;
- thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
- thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
+ br_sample.cpumode = sample->cpumode;
+ br_sample.ip = from;
+ thread__find_symbol_fb(thread, &br_sample, &alf);
+ br_sample.cpumode = sample->cpumode;
+ br_sample.ip = to;
+ thread__find_symbol_fb(thread, &br_sample, &alt);
printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
if (PRINT_FIELD(DSO))
@@ -1140,6 +1150,7 @@ static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
{
struct branch_stack *br = sample->branch_stack;
struct branch_entry *entries = perf_sample__branch_entries(sample);
+ struct perf_sample br_sample = {};
u64 i, from, to;
int printed = 0;
@@ -1154,11 +1165,15 @@ static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
from = entries[i].from;
to = entries[i].to;
- if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
+ br_sample.cpumode = sample->cpumode;
+ br_sample.ip = from;
+ if (thread__find_map_fb(thread, &br_sample, &alf) &&
!dso__adjust_symbols(map__dso(alf.map)))
from = map__dso_map_ip(alf.map, from);
- if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
+ br_sample.cpumode = sample->cpumode;
+ br_sample.ip = to;
+ if (thread__find_map_fb(thread, &br_sample, &alt) &&
!dso__adjust_symbols(map__dso(alt.map)))
to = map__dso_map_ip(alt.map, to);
@@ -1216,7 +1231,18 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,
}
addr_location__init(&al);
- if (!thread__find_map(thread, *cpumode, start, &al) || (dso = map__dso(al.map)) == NULL) {
+ {
+ struct perf_sample sample = {
+ .cpumode = *cpumode,
+ .ip = start,
+ };
+
+ if (!thread__find_map(thread, &sample, &al))
+ dso = NULL;
+ else
+ dso = map__dso(al.map);
+ }
+ if (!dso) {
pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
goto out;
}
@@ -1291,7 +1317,7 @@ static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
int ret = 0;
addr_location__init(&al);
- thread__find_map(thread, cpumode, addr, &al);
+ thread__find_map(thread, &(struct perf_sample){.cpumode = cpumode, .ip = addr}, &al);
if (!al.map)
goto out;
ret = map__fprintf_srccode(al.map, al.addr, stdout,
@@ -1346,7 +1372,9 @@ static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
struct addr_location al;
addr_location__init(&al);
- thread__find_map(thread, x->cpumode, ip, &al);
+ thread__find_map(thread,
+ &(struct perf_sample){.cpumode = x->cpumode,
+ .ip = ip}, &al);
printed += map__fprintf_srcline(al.map, al.addr, " srcline: ", fp);
printed += fprintf(fp, "\t");
addr_location__exit(&al);
@@ -1406,7 +1434,7 @@ static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
int off, printed = 0, ret = 0;
addr_location__init(&al);
- thread__find_map(thread, cpumode, addr, &al);
+ thread__find_map(thread, &(struct perf_sample){.cpumode = cpumode, .ip = addr}, &al);
if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
goto out;
diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c
index 69cc89232d6c..16cf964f1f73 100644
--- a/tools/perf/builtin-timechart.c
+++ b/tools/perf/builtin-timechart.c
@@ -517,6 +517,7 @@ static char *cat_backtrace(struct perf_sample *sample,
size_t p_len;
u8 cpumode = PERF_RECORD_MISC_USER;
struct ip_callchain *chain = sample->callchain;
+ struct perf_sample ip_sample = {};
FILE *f = open_memstream(&p, &p_len);
bool corrupted = false;
@@ -565,7 +566,9 @@ static char *cat_backtrace(struct perf_sample *sample,
addr_location__init(&tal);
tal.filtered = 0;
- if (thread__find_symbol(al.thread, cpumode, ip, &tal))
+ ip_sample.cpumode = cpumode;
+ ip_sample.ip = ip;
+ if (thread__find_symbol(al.thread, &ip_sample, &tal))
fprintf(f, "..... %016" PRIx64 " %s\n", ip, tal.sym->name);
else
fprintf(f, "..... %016" PRIx64 "\n", ip);
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index 4094141461f9..a6df1633bd13 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -3718,7 +3718,7 @@ static int trace__pgfault(struct trace *trace,
if (trace->summary_only)
goto out;
- thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
+ thread__find_symbol(thread, sample, &al);
trace__fprintf_entry_head(trace, thread, 0, true, sample->time,
sample->cpu, trace->output);
@@ -3731,10 +3731,14 @@ static int trace__pgfault(struct trace *trace,
fprintf(trace->output, "] => ");
- thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
+ thread__find_symbol(thread,
+ &(struct perf_sample){.cpumode = sample->cpumode,
+ .ip = sample->addr}, &al);
if (!al.map) {
- thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
+ thread__find_symbol(thread,
+ &(struct perf_sample){.cpumode = sample->cpumode,
+ .ip = sample->addr}, &al);
if (al.map)
map_type = 'x';
diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c
index 7b80c4ab6916..50565c1e0cd0 100644
--- a/tools/perf/tests/code-reading.c
+++ b/tools/perf/tests/code-reading.c
@@ -387,6 +387,10 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
u64 skip_addr;
const char *objdump_name;
char decomp_name[KMOD_DECOMP_LEN];
+ struct perf_sample sample = {
+ .cpumode = cpumode,
+ .ip = addr,
+ };
bool decomp = false;
int ret, err = 0;
struct dso *dso;
@@ -394,7 +398,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr);
addr_location__init(&al);
- if (!thread__find_map(thread, cpumode, addr, &al) || !map__dso(al.map)) {
+ if (!thread__find_map(thread, &sample, &al) || !map__dso(al.map)) {
if (cpumode == PERF_RECORD_MISC_HYPERVISOR) {
pr_debug("Hypervisor address can not be resolved - skipping\n");
goto out;
diff --git a/tools/perf/tests/mmap-thread-lookup.c b/tools/perf/tests/mmap-thread-lookup.c
index 0c5619c6e6e9..dca8bbfe780a 100644
--- a/tools/perf/tests/mmap-thread-lookup.c
+++ b/tools/perf/tests/mmap-thread-lookup.c
@@ -195,8 +195,10 @@ static int mmap_events(synth_cb synth)
pr_debug("looking for map %p\n", td->map);
- thread__find_map(thread, PERF_RECORD_MISC_USER,
- (unsigned long) (td->map + 1), &al);
+ thread__find_map(thread, &(struct perf_sample){
+ .cpumode = PERF_RECORD_MISC_USER,
+ .ip = (unsigned long) (td->map + 1),
+ }, &al);
thread__put(thread);
@@ -208,7 +210,10 @@ static int mmap_events(synth_cb synth)
}
pr_debug("map %p, addr %" PRIx64 "\n", al.map, map__start(al.map));
+
addr_location__exit(&al);
+ if (err)
+ break;
}
machine__delete(machine);
diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c
index 2332d465f975..d226c92be21c 100644
--- a/tools/perf/util/annotate-data.c
+++ b/tools/perf/util/annotate-data.c
@@ -882,8 +882,9 @@ bool get_global_var_info(struct data_loc_info *dloc, u64 addr,
mem_addr = addr + map__reloc(dloc->ms->map);
addr_location__init(&al);
- sym = thread__find_symbol_fb(dloc->thread, dloc->cpumode,
- mem_addr, &al);
+ sym = thread__find_symbol_fb(dloc->thread,
+ &(struct perf_sample){.cpumode = dloc->cpumode,
+ .ip = mem_addr}, &al);
if (sym) {
*var_name = sym->name;
/* Calculate type offset from the start of variable */
diff --git a/tools/perf/util/aslr.c b/tools/perf/util/aslr.c
index e236d5287043..4466e55f1286 100644
--- a/tools/perf/util/aslr.c
+++ b/tools/perf/util/aslr.c
@@ -148,6 +148,10 @@ static u64 aslr_tool__remap_address(struct aslr_tool *aslr,
struct remap_addresses_key key;
u64 *remapped_invariant_ptr = NULL;
u64 remap_addr = 0;
+ struct perf_sample sample = {
+ .cpumode = cpumode,
+ .ip = addr,
+ };
u8 effective_cpumode = cpumode;
struct dso *dso;
const char *dso_name;
@@ -156,7 +160,7 @@ static u64 aslr_tool__remap_address(struct aslr_tool *aslr,
return 0; /* No thread. */
addr_location__init(&al);
- if (!thread__find_map(aslr_thread, cpumode, addr, &al)) {
+ if (!thread__find_map(aslr_thread, &sample, &al)) {
/*
* If lookup fails with specified cpumode, try fallback to the other space
* to be robust against bad cpumode in samples.
@@ -170,7 +174,8 @@ static u64 aslr_tool__remap_address(struct aslr_tool *aslr,
else if (cpumode == PERF_RECORD_MISC_GUEST_USER)
effective_cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
- if (!thread__find_map(aslr_thread, effective_cpumode, addr, &al)) {
+ sample.cpumode = effective_cpumode;
+ if (!thread__find_map(aslr_thread, &sample, &al)) {
addr_location__exit(&al);
return 0; /* No mmap. */
}
@@ -287,18 +292,25 @@ static u64 aslr_tool__findnew_mapping(struct aslr_tool *aslr,
remap_key.pid = (cpumode == PERF_RECORD_MISC_KERNEL ||
cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ?
kernel_pid : thread__pid(aslr_thread);
- if (thread__find_map(aslr_thread, cpumode, start, &al)) {
- struct dso *dso = map__dso(al.map);
- const char *dso_name = dso ? dso__long_name(dso) : NULL;
-
- remap_key.dso = dso;
- if (dso && !is_anon_memory(dso_name) && !is_no_dso_memory(dso_name))
- remap_key.invariant = map__start(al.map) - map__pgoff(al.map);
- else
- remap_key.invariant = map__start(al.map);
- } else {
- remap_key.dso = NULL;
- remap_key.invariant = start;
+ {
+ struct perf_sample sample = {
+ .cpumode = cpumode,
+ .ip = start,
+ };
+
+ if (thread__find_map(aslr_thread, &sample, &al)) {
+ struct dso *dso = map__dso(al.map);
+ const char *dso_name = dso ? dso__long_name(dso) : NULL;
+
+ remap_key.dso = dso;
+ if (dso && !is_anon_memory(dso_name) && !is_no_dso_memory(dso_name))
+ remap_key.invariant = map__start(al.map) - map__pgoff(al.map);
+ else
+ remap_key.invariant = map__start(al.map);
+ } else {
+ remap_key.dso = NULL;
+ remap_key.invariant = start;
+ }
}
/* The key to look up top allocated address. */
@@ -338,13 +350,17 @@ static u64 aslr_tool__findnew_mapping(struct aslr_tool *aslr,
/* No mmap, create an entry from the top address. */
if (hashmap__find(&aslr->top_addresses, &top_addr_key, &top)) {
struct addr_location prev_al;
+ struct perf_sample prev_sample = {
+ .cpumode = cpumode,
+ .ip = start - 1,
+ };
bool is_contiguous = false;
/* Current max allocated mmap address within the process. */
remap_addr = top->remapped_max;
addr_location__init(&prev_al);
- if (thread__find_map(aslr_thread, cpumode, start - 1, &prev_al)) {
+ if (thread__find_map(aslr_thread, &prev_sample, &prev_al)) {
if (map__end(prev_al.map) == start)
is_contiguous = true;
}
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 02233ef0eaff..6c0d33b1609d 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -69,7 +69,7 @@ int build_id__mark_dso_hit(const struct perf_tool *tool __maybe_unused,
}
addr_location__init(&al);
- if (thread__find_map(thread, sample->cpumode, sample->ip, &al))
+ if (thread__find_map(thread, sample, &al))
dso__set_hit(map__dso(al.map));
addr_location__exit(&al);
diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c
index 95881f15324a..fbad96e1e6a9 100644
--- a/tools/perf/util/capstone.c
+++ b/tools/perf/util/capstone.c
@@ -252,7 +252,9 @@ static size_t print_insn_x86(struct thread *thread, u8 cpumode, struct cs_insn *
addr_location__init(&al);
if (op->type == X86_OP_IMM &&
- thread__find_symbol(thread, cpumode, op->imm, &al)) {
+ thread__find_symbol(thread,
+ &(struct perf_sample){.cpumode = cpumode,
+ .ip = op->imm}, &al)) {
printed += fprintf(fp, "%s ", insn[0].mnemonic);
printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
if (print_opts & PRINT_INSN_IMM_HEX)
diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index 3e01b401ca6f..f04157620ccc 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -1182,9 +1182,15 @@ static u32 __cs_etm__mem_access(struct cs_etm_queue *etmq,
}
cpumode = cs_etm__cpu_mode(etmq, address, el);
+ {
+ struct perf_sample sample = {
+ .cpumode = cpumode,
+ .ip = address,
+ };
- if (!thread__find_map(thread, cpumode, address, &al))
- goto out;
+ if (!thread__find_map(thread, &sample, &al))
+ goto out;
+ }
dso = map__dso(al.map);
if (!dso)
diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-convert-json.c
index 40888b7c4467..8fa7bfdaadff 100644
--- a/tools/perf/util/data-convert-json.c
+++ b/tools/perf/util/data-convert-json.c
@@ -235,7 +235,9 @@ static int process_sample_event(const struct perf_tool *tool,
fputc(',', out);
addr_location__init(&tal);
- ok = thread__find_symbol(al.thread, cpumode, ip, &tal);
+ ok = thread__find_symbol(al.thread,
+ &(struct perf_sample){.cpumode = cpumode,
+ .ip = ip}, &tal);
output_sample_callchain_entry(tool, ip, ok ? &tal : NULL);
addr_location__exit(&tal);
}
diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c
index b7095519419e..31ae1c318d2f 100644
--- a/tools/perf/util/debug.c
+++ b/tools/perf/util/debug.c
@@ -336,10 +336,14 @@ void __dump_stack(FILE *file, void **stackdump, size_t stackdump_size)
for (size_t i = 0; i < stackdump_size; i++) {
struct addr_location al;
u64 addr = (u64)(uintptr_t)stackdump[i];
+ struct perf_sample sample = {
+ .cpumode = PERF_RECORD_MISC_USER,
+ .ip = addr,
+ };
bool printed = false;
addr_location__init(&al);
- if (thread && thread__find_map(thread, PERF_RECORD_MISC_USER, addr, &al)) {
+ if (thread && thread__find_map(thread, &sample, &al)) {
al.sym = map__find_symbol(al.map, al.addr);
if (al.sym) {
fprintf(file, " #%zd %p in %s ", i, stackdump[i], al.sym->name);
diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
index 7e179d1d84d7..c0c8e13d007e 100644
--- a/tools/perf/util/dlfilter.c
+++ b/tools/perf/util/dlfilter.c
@@ -177,7 +177,9 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlf
return -1;
addr_location__init(&al);
- thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);
+ thread__find_symbol_fb(thread,
+ &(struct perf_sample){.cpumode = d->sample->cpumode,
+ .ip = address}, &al);
al_to_d_al(&al, &d_al);
@@ -313,7 +315,9 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)
addr_location__init(&a);
- thread__find_map_fb(al->thread, d->sample->cpumode, ip, &a);
+ thread__find_map_fb(al->thread,
+ &(struct perf_sample){.cpumode = d->sample->cpumode,
+ .ip = ip}, &a);
ret = a.map ? code_read(ip, a.map, d->machine, buf, len) : -1;
addr_location__exit(&a);
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index cefb32833da8..4a6b59fa9d82 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -691,7 +691,7 @@ int perf_event__process(const struct perf_tool *tool __maybe_unused,
return machine__process_event(machine, event, sample);
}
-struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
+struct map *thread__find_map(struct thread *thread, struct perf_sample *sample,
struct addr_location *al)
{
struct maps *maps = thread__maps(thread);
@@ -702,31 +702,31 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
thread__zput(al->thread);
al->thread = thread__get(thread);
- al->addr = addr;
- al->cpumode = cpumode;
+ al->addr = sample->ip;
+ al->cpumode = sample->cpumode;
al->filtered = 0;
- if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
+ if (sample->cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
al->level = 'k';
maps = machine__kernel_maps(machine);
load_map = !symbol_conf.lazy_load_kernel_maps;
- } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
+ } else if (sample->cpumode == PERF_RECORD_MISC_USER && perf_host) {
al->level = '.';
- } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
+ } else if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
al->level = 'g';
maps = machine__kernel_maps(machine);
load_map = !symbol_conf.lazy_load_kernel_maps;
- } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
+ } else if (sample->cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
al->level = 'u';
} else {
al->level = 'H';
- if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
- cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
+ if ((sample->cpumode == PERF_RECORD_MISC_GUEST_USER ||
+ sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
!perf_guest)
al->filtered |= (1 << HIST_FILTER__GUEST);
- if ((cpumode == PERF_RECORD_MISC_USER ||
- cpumode == PERF_RECORD_MISC_KERNEL) &&
+ if ((sample->cpumode == PERF_RECORD_MISC_USER ||
+ sample->cpumode == PERF_RECORD_MISC_KERNEL) &&
!perf_host)
al->filtered |= (1 << HIST_FILTER__HOST);
@@ -751,33 +751,34 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
* because it applies only to the sample 'ip' and not necessary to 'addr' or
* branch stack addresses. If possible, use a fallback to deal with those cases.
*/
-struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
+struct map *thread__find_map_fb(struct thread *thread, struct perf_sample *sample,
struct addr_location *al)
{
- struct map *map = thread__find_map(thread, cpumode, addr, al);
+ struct map *map = thread__find_map(thread, sample, al);
struct machine *machine = maps__machine(thread__maps(thread));
- u8 addr_cpumode = machine__addr_cpumode(machine, cpumode, addr);
+ u8 addr_cpumode = machine__addr_cpumode(machine, sample->cpumode, sample->ip);
- if (map || addr_cpumode == cpumode)
+ if (map || addr_cpumode == sample->cpumode)
return map;
- return thread__find_map(thread, addr_cpumode, addr, al);
+ sample->cpumode = addr_cpumode;
+ return thread__find_map(thread, sample, al);
}
-struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
- u64 addr, struct addr_location *al)
+struct symbol *thread__find_symbol(struct thread *thread, struct perf_sample *sample,
+ struct addr_location *al)
{
al->sym = NULL;
- if (thread__find_map(thread, cpumode, addr, al))
+ if (thread__find_map(thread, sample, al))
al->sym = map__find_symbol(al->map, al->addr);
return al->sym;
}
-struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
- u64 addr, struct addr_location *al)
+struct symbol *thread__find_symbol_fb(struct thread *thread, struct perf_sample *sample,
+ struct addr_location *al)
{
al->sym = NULL;
- if (thread__find_map_fb(thread, cpumode, addr, al))
+ if (thread__find_map_fb(thread, sample, al))
al->sym = map__find_symbol(al->map, al->addr);
return al->sym;
}
@@ -813,7 +814,7 @@ int machine__resolve(struct machine *machine, struct addr_location *al,
return -1;
dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread__tid(thread));
- thread__find_map(thread, sample->cpumode, sample->ip, al);
+ thread__find_map(thread, sample, al);
dso = al->map ? map__dso(al->map) : NULL;
dump_printf(" ...... dso: %s\n",
dso
@@ -931,7 +932,9 @@ bool sample_addr_correlates_sym(struct perf_event_attr *attr)
void thread__resolve(struct thread *thread, struct addr_location *al,
struct perf_sample *sample)
{
- thread__find_map_fb(thread, sample->cpumode, sample->addr, al);
+ thread__find_map_fb(thread,
+ &(struct perf_sample){.cpumode = sample->cpumode,
+ .ip = sample->addr}, al);
al->cpu = sample->cpu;
al->sym = NULL;
diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index e8b9c8a67b0a..62d8ac4a913c 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -752,6 +752,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
{
struct intel_pt_queue *ptq = data;
struct machine *machine = ptq->pt->machine;
+ struct perf_sample sample = {};
struct thread *thread;
struct addr_location al;
unsigned char buf[INTEL_PT_INSN_BUF_SZ];
@@ -809,10 +810,13 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
}
}
+ sample.cpumode = cpumode;
+
while (1) {
struct dso *dso;
- if (!thread__find_map(thread, cpumode, *ip, &al) || !map__dso(al.map)) {
+ sample.ip = *ip;
+ if (!thread__find_map(thread, &sample, &al) || !map__dso(al.map)) {
if (al.map)
intel_pt_log("ERROR: thread has no dso for %#" PRIx64 "\n", *ip);
else
@@ -1007,8 +1011,15 @@ static int __intel_pt_pgd_ip(uint64_t ip, void *data)
return -EINVAL;
addr_location__init(&al);
- if (!thread__find_map(thread, cpumode, ip, &al) || !map__dso(al.map))
- return -EINVAL;
+ {
+ struct perf_sample sample = {
+ .cpumode = cpumode,
+ .ip = ip,
+ };
+
+ if (!thread__find_map(thread, &sample, &al) || !map__dso(al.map))
+ return -EINVAL;
+ }
offset = map__map_ip(al.map, ip);
@@ -3663,7 +3674,12 @@ static int intel_pt_find_map(struct thread *thread, u8 cpumode, u64 addr,
struct addr_location *al)
{
if (!al->map || addr < map__start(al->map) || addr >= map__end(al->map)) {
- if (!thread__find_map(thread, cpumode, addr, al))
+ struct perf_sample sample = {
+ .cpumode = cpumode,
+ .ip = addr,
+ };
+
+ if (!thread__find_map(thread, &sample, al))
return -1;
}
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index c7f5ef32d5bd..a228361a8490 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -2093,6 +2093,7 @@ static void ip__resolve_ams(struct thread *thread,
struct addr_map_symbol *ams,
u64 ip)
{
+ static __thread struct perf_sample sample;
struct addr_location al;
addr_location__init(&al);
@@ -2103,7 +2104,8 @@ static void ip__resolve_ams(struct thread *thread,
* Thus, we have to try consecutively until we find a match
* or else, the symbol is unknown
*/
- thread__find_cpumode_addr_location(thread, ip, /*symbols=*/true, &al);
+ sample.ip = ip;
+ thread__find_cpumode_addr_location(thread, &sample, /*symbols=*/true, &al);
ams->addr = ip;
ams->al_addr = al.addr;
@@ -2120,11 +2122,14 @@ static void ip__resolve_data(struct thread *thread,
u8 m, struct addr_map_symbol *ams,
u64 addr, u64 phys_addr, u64 daddr_page_size)
{
+ static __thread struct perf_sample sample;
struct addr_location al;
addr_location__init(&al);
- thread__find_symbol(thread, m, addr, &al);
+ sample.cpumode = m;
+ sample.ip = addr;
+ thread__find_symbol(thread, &sample, &al);
ams->addr = addr;
ams->al_addr = al.addr;
@@ -2247,6 +2252,7 @@ static int add_callchain_ip(struct thread *thread,
u64 branch_from,
bool symbols)
{
+ static __thread struct perf_sample sample;
struct map_symbol ms = {};
struct addr_location al;
int nr_loop_iter = 0, err = 0;
@@ -2257,8 +2263,9 @@ static int add_callchain_ip(struct thread *thread,
al.filtered = 0;
al.sym = NULL;
al.srcline = NULL;
+ sample.ip = ip;
if (!cpumode) {
- thread__find_cpumode_addr_location(thread, ip, symbols, &al);
+ thread__find_cpumode_addr_location(thread, &sample, symbols, &al);
} else {
if (ip >= PERF_CONTEXT_MAX) {
switch (ip) {
@@ -2287,10 +2294,11 @@ static int add_callchain_ip(struct thread *thread,
}
goto out;
}
+ sample.cpumode = *cpumode;
if (symbols)
- thread__find_symbol(thread, *cpumode, ip, &al);
+ thread__find_symbol(thread, &sample, &al);
else
- thread__find_map(thread, *cpumode, ip, &al);
+ thread__find_map(thread, &sample, &al);
}
if (al.sym != NULL) {
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 1efbe5bb26b1..f5d9e7152ef4 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -877,7 +877,10 @@ static PyObject *pyrf_sample_event__srccode(PyObject *self, PyObject *args)
if (addr != pevent->sample->ip) {
addr_location__init(&al);
- thread__find_symbol_fb(pevent->al.thread, pevent->sample->cpumode, addr, &al);
+ thread__find_symbol_fb(pevent->al.thread,
+ &(struct perf_sample){
+ .cpumode = pevent->sample->cpumode,
+ .ip = addr}, &al);
} else {
addr_location__init(&al);
al.thread = thread__get(pevent->al.thread);
@@ -1303,8 +1306,12 @@ static int pyrf_sample_event__resolve_addr_al(struct pyrf_event *pevent,
if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.thread)
return -1;
- thread__find_symbol_fb(pevent->al.thread, pevent->sample->cpumode,
- pevent->sample->addr, addr_al);
+ thread__find_symbol_fb(pevent->al.thread,
+ &(struct perf_sample){
+ .cpumode = pevent->sample->cpumode,
+ .ip = pevent->sample->addr,
+ },
+ addr_al);
return 0;
}
@@ -4481,6 +4488,7 @@ static PyObject *pyrf_call_path__to_callchain(const struct call_path *cp,
struct thread *thread)
{
struct pyrf_callchain *pchain;
+ struct perf_sample sample = {};
const struct call_path *pos;
u64 nr_frames = 0;
@@ -4513,7 +4521,9 @@ static PyObject *pyrf_call_path__to_callchain(const struct call_path *cp,
: PERF_RECORD_MISC_USER;
addr_location__init(&al);
- thread__find_map(thread, cpumode, pos->ip, &al);
+ sample.cpumode = cpumode;
+ sample.ip = pos->ip;
+ thread__find_map(thread, &sample, &al);
frame->map = map__get(al.map);
if (frame->map)
frame->sym = pos->sym;
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 91118c301913..ad3f1ae8a92e 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -413,7 +413,7 @@ int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bo
return thread__clone_maps(thread, parent, do_maps_clone);
}
-void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
+void thread__find_cpumode_addr_location(struct thread *thread, struct perf_sample *sample,
bool symbols, struct addr_location *al)
{
size_t i;
@@ -425,10 +425,11 @@ void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
};
for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
+ sample->cpumode = cpumodes[i];
if (symbols)
- thread__find_symbol(thread, cpumodes[i], addr, al);
+ thread__find_symbol(thread, sample, al);
else
- thread__find_map(thread, cpumodes[i], addr, al);
+ thread__find_map(thread, sample, al);
if (al->map)
break;
@@ -579,6 +580,7 @@ int thread__memcpy(struct thread *thread, struct machine *machine,
void *buf, u64 ip, int len, bool *is64bit)
{
u8 cpumode = PERF_RECORD_MISC_USER;
+ struct perf_sample sample = { .ip = ip };
struct addr_location al;
struct dso *dso;
long offset;
@@ -587,7 +589,8 @@ int thread__memcpy(struct thread *thread, struct machine *machine,
cpumode = PERF_RECORD_MISC_KERNEL;
addr_location__init(&al);
- if (!thread__find_map(thread, cpumode, ip, &al)) {
+ sample.cpumode = cpumode;
+ if (!thread__find_map(thread, &sample, &al)) {
addr_location__exit(&al);
return -1;
}
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index d82fce8173ae..f0c50eafeee5 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -124,17 +124,17 @@ size_t thread__fprintf(struct thread *thread, FILE *fp);
struct thread *thread__main_thread(struct machine *machine, struct thread *thread);
-struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
+struct map *thread__find_map(struct thread *thread, struct perf_sample *sample,
struct addr_location *al);
-struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
+struct map *thread__find_map_fb(struct thread *thread, struct perf_sample *sample,
struct addr_location *al);
-struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
- u64 addr, struct addr_location *al);
-struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
- u64 addr, struct addr_location *al);
+struct symbol *thread__find_symbol(struct thread *thread, struct perf_sample *sample,
+ struct addr_location *al);
+struct symbol *thread__find_symbol_fb(struct thread *thread, struct perf_sample *sample,
+ struct addr_location *al);
-void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
+void thread__find_cpumode_addr_location(struct thread *thread, struct perf_sample *sample,
bool symbols, struct addr_location *al);
int thread__memcpy(struct thread *thread, struct machine *machine,
diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c
index d49901b1571e..855faa54c0cf 100644
--- a/tools/perf/util/unwind-libdw.c
+++ b/tools/perf/util/unwind-libdw.c
@@ -82,7 +82,9 @@ static int __report_module(struct addr_location *al, u64 ip,
* Some callers will use al->sym, so we can't just use the
* cheaper thread__find_map() here.
*/
- thread__find_symbol(ui->thread, PERF_RECORD_MISC_USER, ip, al);
+ thread__find_symbol(ui->thread,
+ &(struct perf_sample){.cpumode = PERF_RECORD_MISC_USER, .ip = ip},
+ al);
if (al->map)
dso = map__dso(al->map);
@@ -208,13 +210,14 @@ static bool get_thread(Dwfl *dwfl __maybe_unused, pid_t tid, void *arg,
static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
Dwarf_Word *data, size_t len)
{
+ struct perf_sample sample = { .cpumode = PERF_RECORD_MISC_USER, .ip = addr };
struct addr_location al;
union u64_swap u;
ssize_t size;
struct dso *dso;
addr_location__init(&al);
- if (!thread__find_map(ui->thread, PERF_RECORD_MISC_USER, addr, &al)) {
+ if (!thread__find_map(ui->thread, &sample, &al)) {
pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
goto out_fail;
}
diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-libunwind.c
index 6052383e862b..83f043f266da 100644
--- a/tools/perf/util/unwind-libunwind.c
+++ b/tools/perf/util/unwind-libunwind.c
@@ -370,7 +370,9 @@ static struct map *find_map(uint64_t ip, struct unwind_info *ui)
struct map *ret;
addr_location__init(&al);
- thread__find_map(ui->thread, PERF_RECORD_MISC_USER, ip, &al);
+ thread__find_map(ui->thread,
+ &(struct perf_sample){.cpumode = PERF_RECORD_MISC_USER, .ip = ip},
+ &al);
ret = map__get(al.map);
addr_location__exit(&al);
return ret;
@@ -626,7 +628,9 @@ static int entry(uint64_t ip, struct thread *thread, unwind_entry_cb_t cb, void
int ret;
addr_location__init(&al);
- e.ms.sym = thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al);
+ e.ms.sym = thread__find_symbol(thread,
+ &(struct perf_sample){.cpumode = PERF_RECORD_MISC_USER,
+ .ip = ip}, &al);
e.ip = ip;
e.ms.map = al.map;
e.ms.thread = thread__get(al.thread);
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 13/19] perf maps: Add dsos__find_by_build_id() and maps__find_by_build_id()
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (11 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 12/19] perf: Refactor thread map and symbol APIs to take perf_sample Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 14/19] perf evsel: Parse, synthesize, and dump BUILD_ID_OFFSET sample types Ian Rogers
` (5 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Add helpers to look up a DSO in struct dsos by its build ID and to find
(or lazily populate from machine->dsos or the build-id cache) a struct
map in struct maps matching a given build ID. This prepares for
resolving PERF_SAMPLE_BUILD_ID_OFFSET and
PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET samples without virtual-address
MMAP records.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/dsos.c | 68 +++++++++++++++++++++++++++++++++++++++
tools/perf/util/dsos.h | 6 ++++
tools/perf/util/maps.c | 73 ++++++++++++++++++++++++++++++++++++++++++
tools/perf/util/maps.h | 2 ++
4 files changed, 149 insertions(+)
diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c
index e927e707abac..60178a093f4e 100644
--- a/tools/perf/util/dsos.c
+++ b/tools/perf/util/dsos.c
@@ -294,6 +294,40 @@ struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
return res;
}
+struct dsos__find_build_id_cb_args {
+ const struct build_id *bid;
+ enum dso_space_type dso_space;
+ struct dso *res;
+};
+
+static int dsos__find_build_id_cb(struct dso *dso, void *data)
+{
+ struct dsos__find_build_id_cb_args *args = data;
+
+ if (dso__kernel(dso) == args->dso_space &&
+ dso__has_build_id(dso) && dso__build_id_equal(dso, args->bid)) {
+ args->res = dso__get(dso);
+ return 1;
+ }
+ return 0;
+}
+
+struct dso *dsos__find_by_build_id(struct dsos *dsos, const struct build_id *bid,
+ int dso_space)
+{
+ struct dsos__find_build_id_cb_args args = {
+ .bid = bid,
+ .dso_space = dso_space,
+ .res = NULL,
+ };
+
+ if (!build_id__is_defined(bid))
+ return NULL;
+
+ dsos__for_each_dso(dsos, dsos__find_build_id_cb, &args);
+ return args.res;
+}
+
static void dso__set_basename(struct dso *dso)
{
bool allocated = false;
@@ -349,6 +383,40 @@ struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct d
return dso;
}
+struct dso *dsos__findnew_id_space(struct dsos *dsos, struct machine *machine,
+ const char *name, const struct dso_id *id,
+ int dso_space)
+{
+ enum dso_space_type space = dso_space;
+ struct dso *dso;
+
+ down_write(&dsos->lock);
+ dso = __dsos__find_id(dsos, name, id, false, /*write_locked=*/true);
+ if (dso) {
+ if (dso__kernel(dso) == space)
+ __dso__improve_id(dso, id);
+ else
+ dso__zput(dso);
+ } else {
+ dso = dso__new_id(name, id);
+ if (dso) {
+ dso__set_basename(dso);
+ if (space != DSO_SPACE__USER) {
+ struct kmod_path m = { .name = NULL };
+
+ if (!kmod_path__parse_name(&m, name) && m.kmod)
+ dso__set_module_info(dso, &m, machine);
+ dso__set_kernel(dso, space);
+ free(m.name);
+ }
+ if (__dsos__add(dsos, dso))
+ dso__zput(dso);
+ }
+ }
+ up_write(&dsos->lock);
+ return dso;
+}
+
struct dsos__fprintf_buildid_cb_args {
FILE *fp;
bool (*skip)(struct dso *dso, int parm);
diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h
index a26774950866..6343bf96b45a 100644
--- a/tools/perf/util/dsos.h
+++ b/tools/perf/util/dsos.h
@@ -8,6 +8,7 @@
#include <linux/rbtree.h>
#include "rwsem.h"
+struct build_id;
struct dso;
struct dso_id;
struct kmod_path;
@@ -31,8 +32,13 @@ void dsos__exit(struct dsos *dsos);
int __dsos__add(struct dsos *dsos, struct dso *dso);
int dsos__add(struct dsos *dsos, struct dso *dso);
struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short);
+struct dso *dsos__find_by_build_id(struct dsos *dsos, const struct build_id *bid,
+ int dso_space);
struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id);
+struct dso *dsos__findnew_id_space(struct dsos *dsos, struct machine *machine,
+ const char *name, const struct dso_id *id,
+ int dso_space);
bool dsos__read_build_ids(struct dsos *dsos, bool with_hits);
diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c
index 675fe0223faa..3ce6feb23821 100644
--- a/tools/perf/util/maps.c
+++ b/tools/perf/util/maps.c
@@ -2,8 +2,11 @@
#include <errno.h>
#include <stdlib.h>
#include <linux/zalloc.h>
+#include "build-id.h"
#include "debug.h"
#include "dso.h"
+#include "dsos.h"
+#include "machine.h"
#include "map.h"
#include "maps.h"
#include "rwsem.h"
@@ -1252,6 +1255,76 @@ static int map__strcmp_name(const void *name, const void *b)
return strcmp(name, dso__short_name(dso));
}
+struct map *maps__find_by_build_id(struct maps *maps, struct build_id *bid)
+{
+ struct machine *machine = maps__machine(maps);
+ bool is_kmaps = RC_CHK_EQUAL(maps, machine__kernel_maps(machine));
+ enum dso_space_type dso_space = is_kmaps
+ ? (machine__is_host(machine) ? DSO_SPACE__KERNEL : DSO_SPACE__KERNEL_GUEST)
+ : DSO_SPACE__USER;
+ struct map *result = NULL;
+
+ if (!build_id__is_defined(bid))
+ return NULL;
+
+ down_read(maps__lock(maps));
+ for (unsigned int i = 0, n = maps__nr_maps(maps); i < n; i++) {
+ struct map *map = maps__maps_by_address(maps)[i];
+ struct dso *dso = map__dso(map);
+
+ if (dso && dso__kernel(dso) == dso_space &&
+ dso__has_build_id(dso) &&
+ dso__build_id_equal(dso, bid)) {
+ result = map__get(map);
+ break;
+ }
+ }
+ up_read(maps__lock(maps));
+
+ if (!result) {
+ struct dso *dso = dsos__find_by_build_id(&machine->dsos, bid, dso_space);
+
+ if (!dso) {
+ char sbuild_id[SBUILD_ID_SIZE];
+ char *origname;
+
+ build_id__snprintf(bid, sbuild_id, sizeof(sbuild_id));
+ origname = build_id_cache__origname(sbuild_id);
+ if (origname) {
+ struct dso_id id = { .build_id = *bid };
+
+ dso = dsos__findnew_id_space(&machine->dsos, machine,
+ origname, &id, dso_space);
+ free(origname);
+ }
+ }
+ if (dso) {
+ down_write(maps__lock(maps));
+ for (unsigned int i = 0, n = maps__nr_maps(maps); i < n; i++) {
+ struct map *map = maps__maps_by_address(maps)[i];
+ struct dso *map_dso = map__dso(map);
+
+ if (map_dso && dso__kernel(map_dso) == dso_space &&
+ dso__has_build_id(map_dso) &&
+ dso__build_id_equal(map_dso, bid)) {
+ result = map__get(map);
+ break;
+ }
+ }
+ if (!result) {
+ result = map__new2(0, dso);
+ if (result && __maps__insert(maps, result)) {
+ map__put(result);
+ result = NULL;
+ }
+ }
+ up_write(maps__lock(maps));
+ dso__put(dso);
+ }
+ }
+ return result;
+}
+
struct map *maps__find_by_name(struct maps *maps, const char *name)
{
struct map *result = NULL;
diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h
index 4ec9b7453a3b..6f1791457068 100644
--- a/tools/perf/util/maps.h
+++ b/tools/perf/util/maps.h
@@ -11,6 +11,7 @@ struct ref_reloc_sym;
struct machine;
struct map;
struct maps;
+struct build_id;
#define KMAP_NAME_LEN 256
@@ -76,6 +77,7 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams);
int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new);
struct map *maps__find_by_name(struct maps *maps, const char *name);
+struct map *maps__find_by_build_id(struct maps *maps, struct build_id *bid);
struct map *maps__find_next_entry(struct maps *maps, struct map *map);
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 14/19] perf evsel: Parse, synthesize, and dump BUILD_ID_OFFSET sample types
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (12 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 13/19] perf maps: Add dsos__find_by_build_id() and maps__find_by_build_id() Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 15/19] perf symbol: Resolve symbols and callchains using build ID and offset Ian Rogers
` (4 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Add fields to struct perf_sample and implement parsing, synthesis,
cross-endian byte-swapping, and raw trace dumping ('perf report -D') for
PERF_SAMPLE_BUILD_ID_OFFSET, PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET, and
PERF_RECORD_CALLCHAIN_DEFERRED with PERF_RECORD_MISC_MMAP_BUILD_ID.
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/evsel.c | 153 +++++++++++++++++++++-
tools/perf/util/evsel.h | 3 +-
tools/perf/util/perf_event_attr_fprintf.c | 3 +-
tools/perf/util/python.c | 17 +++
tools/perf/util/sample.c | 84 ++++++++++++
tools/perf/util/sample.h | 39 ++++++
tools/perf/util/session.c | 126 ++++++++++++++++--
tools/perf/util/synthetic-events.c | 41 ++++++
8 files changed, 444 insertions(+), 22 deletions(-)
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 322582bb4f40..49f6bbf08160 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -3445,6 +3445,89 @@ static int __set_offcpu_sample(struct perf_sample *data)
return -EFAULT;
}
+/*
+ * Before __evsel__parse_sample() is called on a cross-endian perf.data file,
+ * perf_event__all64_swap() has already byte-swapped the entire PERF_RECORD_SAMPLE
+ * payload in 64-bit chunks. That leaves 64-bit fields (like offset, ip, cookie)
+ * in host endianness, but scrambles the 24-byte struct perf_build_id (which
+ * contains a 16-bit reserved field, a 1-byte size, and a 20-byte byte array).
+ * Undo the 64-bit chunk swap on struct perf_build_id and byte-swap its 16-bit
+ * field.
+ */
+static void perf_build_id__undo_bswap_64(struct perf_build_id *bid)
+{
+ mem_bswap_64(bid, sizeof(*bid));
+ bid->__reserved_2 = bswap_16(bid->__reserved_2);
+}
+
+static int parse_callchain_build_id_offset(const __u64 **arrayp, const void *endp,
+ struct perf_sample *data, bool swapped)
+{
+ const __u64 *array = *arrayp;
+ __u64 *hdr;
+ union u64_swap u;
+ u32 nr_build_ids, nr_offsets;
+ u32 pos = 0, aligned_bids_len;
+ u8 *raw_bytes;
+ u8 *build_ids = NULL;
+ u64 *offsets = NULL;
+
+ if ((const void *)(array + 1) > endp)
+ return -EFAULT;
+
+ hdr = (__u64 *)array++;
+ u.val64 = *hdr;
+ if (swapped) {
+ /* undo swap of words, then swap_32 */
+ u.val64 = bswap_64(u.val64);
+ u.val32[0] = bswap_32(u.val32[0]);
+ u.val32[1] = bswap_32(u.val32[1]);
+ }
+ nr_build_ids = u.val32[0];
+ nr_offsets = u.val32[1];
+ if (nr_build_ids > 254)
+ return -EFAULT;
+
+ raw_bytes = (u8 *)array;
+ for (u32 i = 0; i < nr_build_ids; i++) {
+ u32 byte_idx = swapped ? (pos ^ 7) : pos;
+ u8 bid_size;
+
+ if ((const void *)(raw_bytes + (pos | 7) + 1) > endp)
+ return -EFAULT;
+ bid_size = raw_bytes[byte_idx];
+ if (bid_size == 0 || bid_size > 20)
+ return -EFAULT;
+ pos += 1 + bid_size;
+ }
+ aligned_bids_len = ALIGN(pos, sizeof(u64));
+ if ((const void *)(raw_bytes + aligned_bids_len) > endp)
+ return -EFAULT;
+
+ array += aligned_bids_len / sizeof(u64);
+ if (nr_offsets > (size_t)((const u8 *)endp - (const u8 *)array) / sizeof(u64))
+ return -EFAULT;
+
+ if (swapped) {
+ *hdr = u.val64;
+ if (aligned_bids_len > 0)
+ mem_bswap_64(raw_bytes, aligned_bids_len);
+ }
+ if (aligned_bids_len > 0)
+ build_ids = raw_bytes;
+ if (nr_offsets > 0)
+ offsets = (u64 *)array;
+ array += nr_offsets;
+
+ data->callchain_bid.nr_build_ids = nr_build_ids;
+ data->callchain_bid.nr_offsets = nr_offsets;
+ data->callchain_bid.build_ids_len = aligned_bids_len;
+ data->callchain_bid.build_ids = build_ids;
+ data->callchain_bid.offsets = offsets;
+ *arrayp = array;
+ return 0;
+}
+
int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,
struct perf_sample *data, bool needs_swap)
{
@@ -3472,14 +3555,21 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,
data->vcpu = -1;
if (event->header.type == PERF_RECORD_CALLCHAIN_DEFERRED) {
- const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
-
OVERFLOW_CHECK_u64(&event->callchain_deferred.nr);
- data->callchain = (struct ip_callchain *)&event->callchain_deferred.nr;
- if (data->callchain->nr > max_callchain_nr)
- goto out_efault;
- OVERFLOW_CHECK(event->callchain_deferred.ips,
- data->callchain->nr * sizeof(u64), max_size);
+ if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
+ array = (const __u64 *)&event->callchain_deferred.nr_build_ids;
+ if (parse_callchain_build_id_offset(&array, endp, data,
+ /*swapped=*/false))
+ goto out_efault;
+ } else {
+ const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
+
+ data->callchain = (struct ip_callchain *)&event->callchain_deferred.nr;
+ if (data->callchain->nr > max_callchain_nr)
+ goto out_efault;
+ OVERFLOW_CHECK(event->callchain_deferred.ips,
+ data->callchain->nr * sizeof(u64), max_size);
+ }
data->deferred_cookie = event->callchain_deferred.cookie;
@@ -3918,6 +4008,55 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,
array = (void *)array + sz;
}
+ if (type & PERF_SAMPLE_BUILD_ID_OFFSET) {
+ const struct perf_sample_build_id_offset *entry =
+ (const struct perf_sample_build_id_offset *)array;
+
+ OVERFLOW_CHECK(array, sizeof(*entry), max_size);
+ if (evsel->core.attr.defer_callchain && entry->zero == 0 &&
+ entry->context == PERF_CONTEXT_USER_DEFERRED) {
+ data->deferred_bid = true;
+ data->deferred_callchain = true;
+ data->deferred_cookie = entry->cookie;
+ data->bid.bid = (struct perf_build_id *)&entry->bid;
+ data->bid.offset = entry->ip;
+ } else {
+ struct perf_build_id *bid =
+ (struct perf_build_id *)&entry->bid;
+
+ /*
+ * perf_event__all64_swap() already swapped the sample
+ * in 64-bit chunks, so entry->offset is already in host
+ * endianness; undo the 64-bit swap on the build ID
+ * bytes in-place so re-parsing with needs_swap=false
+ * sees host layout.
+ */
+ if (swapped)
+ perf_build_id__undo_bswap_64(bid);
+ data->bid.bid = bid;
+ data->bid.offset = entry->offset;
+ }
+ if (!(type & PERF_SAMPLE_IP))
+ data->ip = data->bid.offset;
+ array += sizeof(*entry) / sizeof(u64);
+ } else {
+ data->bid.bid = NULL;
+ data->bid.offset = 0;
+ }
+
+ if (type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) {
+ if (parse_callchain_build_id_offset(&array, endp, data, swapped))
+ goto out_efault;
+ if (evsel->core.attr.defer_callchain &&
+ data->callchain_bid.nr_offsets >= 2 &&
+ data->callchain_bid.offsets[data->callchain_bid.nr_offsets - 2] ==
+ PERF_CONTEXT_USER_DEFERRED) {
+ data->deferred_cookie =
+ data->callchain_bid.offsets[data->callchain_bid.nr_offsets - 1];
+ data->deferred_callchain = true;
+ }
+ }
+
if (evsel__is_offcpu_event(evsel)) {
if (__set_offcpu_sample(data))
goto out_efault;
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 53f3641b66e1..858c32bb02e2 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -570,7 +570,8 @@ static inline bool evsel__has_callchain(const struct evsel *evsel)
* For reporting purposes, an evsel sample can have a recorded callchain
* or a callchain synthesized from AUX area data.
*/
- return evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN ||
+ return evsel->core.attr.sample_type & (PERF_SAMPLE_CALLCHAIN |
+ PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) ||
evsel->synth_sample_type & PERF_SAMPLE_CALLCHAIN;
}
diff --git a/tools/perf/util/perf_event_attr_fprintf.c b/tools/perf/util/perf_event_attr_fprintf.c
index 2e8be9a357dc..dd653da923c4 100644
--- a/tools/perf/util/perf_event_attr_fprintf.c
+++ b/tools/perf/util/perf_event_attr_fprintf.c
@@ -40,7 +40,8 @@ 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(BUILD_ID_OFFSET),
+ bit_name(CALLCHAIN_BUILD_ID_OFFSET),
{ .name = NULL, }
};
#undef bit_name
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index f5d9e7152ef4..bf892b3a8736 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -1747,6 +1747,18 @@ static int pyrf_event__copy(struct pyrf_event *pevent)
sizeof(pevent->sample_storage.insn));
pevent->sample_storage.insn_len = orig_sample->insn_len;
}
+ if (orig_sample->bid.bid ==
+ (const struct perf_build_id *)orig_sample->bid.deferred_storage) {
+ memcpy(pevent->sample_storage.bid.deferred_storage,
+ orig_sample->bid.deferred_storage,
+ sizeof(pevent->sample_storage.bid.deferred_storage));
+ pevent->sample_storage.bid.bid =
+ (struct perf_build_id *)
+ pevent->sample_storage.bid.deferred_storage;
+ pevent->sample_storage.bid.offset = orig_sample->bid.offset;
+ pevent->sample_storage.ip = orig_sample->ip;
+ pevent->sample_storage.deferred_bid = false;
+ }
}
pevent->sample = &pevent->sample_storage;
} else if (orig_sample != &pevent->sample_storage) {
@@ -1763,6 +1775,11 @@ static int pyrf_event__copy(struct pyrf_event *pevent)
evsel__get(pevent->sample_storage.evsel);
pevent->sample_storage.callchain = NULL;
pevent->sample_storage.merged_callchain = false;
+ pevent->sample_storage.bid.bid = NULL;
+ memset(&pevent->sample_storage.callchain_bid, 0,
+ sizeof(pevent->sample_storage.callchain_bid));
+ pevent->sample_storage.allocated_callchain_bids = false;
+ pevent->sample_storage.merged_callchain_bids = false;
pevent->sample_storage.raw_data = NULL;
pevent->sample_storage.raw_size = 0;
pevent->sample_storage.branch_stack = NULL;
diff --git a/tools/perf/util/sample.c b/tools/perf/util/sample.c
index 4abb689132bc..0becb78d70d6 100644
--- a/tools/perf/util/sample.c
+++ b/tools/perf/util/sample.c
@@ -5,7 +5,9 @@
#include <string.h>
#include <elf.h>
+#include <linux/bits.h>
#include <linux/zalloc.h>
+#include <perf/event.h>
#include "../../arch/x86/include/asm/insn.h"
#include "debug.h"
@@ -24,12 +26,16 @@ void perf_sample__init(struct perf_sample *sample, bool all)
if (all) {
memset(sample, 0, sizeof(*sample));
} else {
+ sample->ip = 0;
sample->evsel = NULL;
sample->user_regs = NULL;
sample->intr_regs = NULL;
sample->merged_callchain = false;
sample->callchain = NULL;
sample->ret_addr = 0;
+ sample->allocated_callchain_bids = false;
+ sample->merged_callchain_bids = false;
+ memset(&sample->callchain_bid, 0, sizeof(sample->callchain_bid));
}
}
@@ -43,6 +49,84 @@ void perf_sample__exit(struct perf_sample *sample)
zfree(&sample->callchain);
sample->merged_callchain = false;
}
+ if (sample->allocated_callchain_bids) {
+ zfree(&sample->callchain_bid.build_ids);
+ memset(&sample->callchain_bid, 0, sizeof(sample->callchain_bid));
+ sample->allocated_callchain_bids = false;
+ sample->merged_callchain_bids = false;
+ }
+}
+
+void perf_sample__callchain_bid_entry(const struct perf_sample *sample,
+ u32 idx, struct perf_build_id *bid_out,
+ u64 *offset_out)
+{
+ bool is_user_ctx = true;
+ u64 raw_offset;
+ u8 bid_idx;
+ u32 pos = 0;
+ u8 sz;
+
+ if (bid_out)
+ memset(bid_out, 0, sizeof(*bid_out));
+ if (idx >= sample->callchain_bid.nr_offsets ||
+ !sample->callchain_bid.offsets) {
+ *offset_out = 0;
+ return;
+ }
+
+ raw_offset = sample->callchain_bid.offsets[idx];
+ if (raw_offset >= PERF_CONTEXT_MAX) {
+ *offset_out = raw_offset;
+ return;
+ }
+
+ for (u32 k = 0; k < idx; k++) {
+ u64 v = sample->callchain_bid.offsets[k];
+
+ if (v < PERF_CONTEXT_MAX)
+ continue;
+ if (v == PERF_CONTEXT_USER_DEFERRED) {
+ is_user_ctx = true;
+ if (!sample->merged_callchain_bids) {
+ if (k + 1 == idx) {
+ *offset_out = raw_offset;
+ return;
+ }
+ k++;
+ }
+ } else if (v == PERF_CONTEXT_USER ||
+ v == PERF_CONTEXT_GUEST_USER) {
+ is_user_ctx = true;
+ } else {
+ is_user_ctx = false;
+ }
+ }
+
+ if (!is_user_ctx) {
+ *offset_out = raw_offset;
+ return;
+ }
+
+ bid_idx = raw_offset >> 56;
+ if (bid_idx == 0 || bid_idx == 0xff ||
+ bid_idx > sample->callchain_bid.nr_build_ids ||
+ !sample->callchain_bid.build_ids) {
+ *offset_out = raw_offset;
+ return;
+ }
+
+ if (bid_out) {
+ for (u32 b = 0; b < (u32)(bid_idx - 1); b++)
+ pos += 1 + sample->callchain_bid.build_ids[pos];
+
+ sz = sample->callchain_bid.build_ids[pos];
+ if (sz > sizeof(bid_out->data))
+ sz = sizeof(bid_out->data);
+ bid_out->size = sz;
+ memcpy(bid_out->data, &sample->callchain_bid.build_ids[pos + 1], sz);
+ }
+ *offset_out = raw_offset & GENMASK_ULL(55, 0);
}
struct regs_dump *perf_sample__user_regs(struct perf_sample *sample)
diff --git a/tools/perf/util/sample.h b/tools/perf/util/sample.h
index 865ed18200f1..d5c52df508c4 100644
--- a/tools/perf/util/sample.h
+++ b/tools/perf/util/sample.h
@@ -8,6 +8,7 @@
struct evsel;
struct machine;
struct thread;
+struct perf_build_id;
/* number of register is bound by the number of bits in regs_dump::mask (64) */
#define PERF_SAMPLE_REGS_CACHE_SIZE (8 * sizeof(u64))
@@ -168,6 +169,26 @@ struct perf_sample {
u64 code_page_size;
/** @cgroup: The sample event PERF_SAMPLE_CGROUP value. */
u64 cgroup;
+ /** @bid: The sample event PERF_SAMPLE_BUILD_ID_OFFSET value. */
+ struct {
+ struct perf_build_id *bid;
+ union {
+ u64 offset;
+ u64 ip;
+ };
+ u64 deferred_storage[3];
+ } bid;
+ /**
+ * @callchain_bid: Build IDs and offsets for the callchain when
+ * PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET is set.
+ */
+ struct {
+ u32 nr_build_ids;
+ u32 nr_offsets;
+ u32 build_ids_len;
+ u8 *build_ids;
+ u64 *offsets;
+ } callchain_bid;
/** @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. */
@@ -210,11 +231,26 @@ struct perf_sample {
* user callchain marker was encountered.
*/
bool deferred_callchain;
+ /**
+ * @deferred_bid: When processing PERF_SAMPLE_BUILD_ID_OFFSET a deferred
+ * user marker was encountered.
+ */
+ bool deferred_bid;
/**
* @merged_callchain: A synthesized merged callchain that is allocated
* and needs freeing.
*/
bool merged_callchain;
+ /**
+ * @allocated_callchain_bids: callchain_bid.build_ids is heap-allocated
+ * and needs freeing.
+ */
+ bool allocated_callchain_bids;
+ /**
+ * @merged_callchain_bids: PERF_CONTEXT_USER_DEFERRED in callchain_bid
+ * has been merged with its deferred callchain.
+ */
+ bool merged_callchain_bids;
/**
* @deferred_cookie: Identifier of the deferred callchain in the later
* PERF_RECORD_CALLCHAIN_DEFERRED event.
@@ -261,6 +297,9 @@ void perf_sample__init(struct perf_sample *sample, bool all);
void perf_sample__exit(struct perf_sample *sample);
struct regs_dump *perf_sample__user_regs(struct perf_sample *sample);
struct regs_dump *perf_sample__intr_regs(struct perf_sample *sample);
+void perf_sample__callchain_bid_entry(const struct perf_sample *sample,
+ u32 idx, struct perf_build_id *bid_out,
+ u64 *offset_out);
void perf_sample__fetch_insn(struct perf_sample *sample,
struct thread *thread,
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index ec1ea510ae61..a332a83dd60e 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -21,6 +21,7 @@
#include "map_symbol.h"
#include "branch.h"
+#include "build-id.h"
#include "debug.h"
#include "dwarf-regs.h"
#include "env.h"
@@ -1186,6 +1187,59 @@ static int perf_event__header_feature_swap(union perf_event *event,
return 0;
}
+static int perf_event__callchain_deferred_swap(union perf_event *event,
+ bool sample_id_all)
+{
+ u32 nr_build_ids, nr_offsets, pos = 0, aligned_bids_len;
+ const void *endp = (void *)event + event->header.size;
+ u8 *build_ids;
+ u64 *offsets;
+
+ if (!(event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID))
+ return perf_event__all64_swap(event, sample_id_all);
+
+ if (event->header.size < sizeof(event->callchain_deferred))
+ return -1;
+
+ event->callchain_deferred.cookie = bswap_64(event->callchain_deferred.cookie);
+ event->callchain_deferred.nr_build_ids =
+ bswap_32(event->callchain_deferred.nr_build_ids);
+ event->callchain_deferred.nr_offsets =
+ bswap_32(event->callchain_deferred.nr_offsets);
+
+ nr_build_ids = event->callchain_deferred.nr_build_ids;
+ nr_offsets = event->callchain_deferred.nr_offsets;
+ if (nr_build_ids > 254)
+ return -1;
+
+ build_ids = event->callchain_deferred.build_ids;
+ for (u32 i = 0; i < nr_build_ids; i++) {
+ u8 bid_size;
+
+ if ((void *)(build_ids + pos + 1) > endp)
+ return -1;
+ bid_size = build_ids[pos];
+ if (bid_size == 0 || bid_size > 20 ||
+ (void *)(build_ids + pos + 1 + bid_size) > endp)
+ return -1;
+ pos += 1 + bid_size;
+ }
+ aligned_bids_len = ALIGN(pos, sizeof(u64));
+ if ((void *)(build_ids + aligned_bids_len) > endp)
+ return -1;
+
+ offsets = (u64 *)(build_ids + aligned_bids_len);
+ if (nr_offsets > (size_t)((const u8 *)endp - (const u8 *)offsets) / sizeof(u64))
+ return -1;
+
+ for (u32 i = 0; i < nr_offsets; i++)
+ offsets[i] = bswap_64(offsets[i]);
+
+ if (sample_id_all)
+ swap_sample_id_all(event, &offsets[nr_offsets]);
+ return 0;
+}
+
typedef int (*perf_event__swap_op)(union perf_event *event,
bool sample_id_all);
@@ -1211,7 +1265,7 @@ static perf_event__swap_op perf_event__swap_ops[] = {
[PERF_RECORD_BPF_EVENT] = perf_event__bpf_event_swap,
[PERF_RECORD_TEXT_POKE] = perf_event__text_poke_swap,
[PERF_RECORD_AUX_OUTPUT_HW_ID] = perf_event__all64_swap,
- [PERF_RECORD_CALLCHAIN_DEFERRED] = perf_event__all64_swap,
+ [PERF_RECORD_CALLCHAIN_DEFERRED] = perf_event__callchain_deferred_swap,
[PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
[PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
@@ -1294,11 +1348,18 @@ static void callchain__lbr_callstack_printf(struct perf_sample *sample)
struct ip_callchain *callchain = sample->callchain;
struct branch_stack *lbr_stack = sample->branch_stack;
struct branch_entry *entries = perf_sample__branch_entries(sample);
- u64 kernel_callchain_nr = callchain->nr;
+ u64 kernel_callchain_nr = callchain ? callchain->nr : sample->callchain_bid.nr_offsets;
unsigned int i;
for (i = 0; i < kernel_callchain_nr; i++) {
- if (callchain->ips[i] == PERF_CONTEXT_USER)
+ u64 ip = 0;
+
+ if (callchain)
+ ip = callchain->ips[i];
+ else
+ perf_sample__callchain_bid_entry(sample, i, NULL, &ip);
+
+ if (ip == PERF_CONTEXT_USER)
break;
}
@@ -1325,9 +1386,16 @@ static void callchain__lbr_callstack_printf(struct perf_sample *sample)
printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
- for (i = 0; i < kernel_callchain_nr; i++)
- printf("..... %2d: %016" PRIx64 "\n",
- i, callchain->ips[i]);
+ for (i = 0; i < kernel_callchain_nr; i++) {
+ u64 ip = 0;
+
+ if (callchain)
+ ip = callchain->ips[i];
+ else
+ perf_sample__callchain_bid_entry(sample, i, NULL, &ip);
+
+ printf("..... %2d: %016" PRIx64 "\n", i, ip);
+ }
printf("..... %2d: %016" PRIx64 "\n",
(int)(kernel_callchain_nr), entries[0].to);
@@ -1368,12 +1436,33 @@ static void callchain__printf(struct evsel *evsel,
if (evsel__has_branch_callstack(evsel))
callchain__lbr_callstack_printf(sample);
- printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
-
- for (i = 0; i < callchain->nr; i++)
- printf("..... %2d: %016" PRIx64 "%s\n",
- i, callchain->ips[i],
- callchain_context_str(callchain->ips[i]));
+ if (callchain) {
+ printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
+
+ for (i = 0; i < callchain->nr; i++)
+ printf("..... %2d: %016" PRIx64 "%s\n",
+ i, callchain->ips[i],
+ callchain_context_str(callchain->ips[i]));
+ } else if (sample->callchain_bid.nr_offsets) {
+ printf("... FP chain (build ID + offset): nr:%u (build IDs:%u)\n",
+ sample->callchain_bid.nr_offsets,
+ sample->callchain_bid.nr_build_ids);
+
+ for (i = 0; i < sample->callchain_bid.nr_offsets; i++) {
+ struct perf_build_id bid;
+ u64 offset = 0;
+ u8 bid_idx = sample->callchain_bid.offsets[i] >> 56;
+
+ perf_sample__callchain_bid_entry(sample, i, &bid, &offset);
+ if (bid.size > 0) {
+ printf("..... %2d: %016" PRIx64 " (build ID %u)\n",
+ i, offset, bid_idx - 1);
+ } else {
+ printf("..... %2d: %016" PRIx64 "%s\n",
+ i, offset, callchain_context_str(offset));
+ }
+ }
+ }
if (sample->deferred_callchain)
printf("...... (deferred)\n");
@@ -1698,6 +1787,17 @@ static void dump_sample(struct machine *machine, union perf_event *event,
event->header.misc, sample->pid, sample->tid, sample->ip,
sample->period, sample->addr);
+ if (sample_type & PERF_SAMPLE_BUILD_ID_OFFSET) {
+ struct build_id bid;
+ char sbuild_id[SBUILD_ID_SIZE] = "";
+
+ if (sample->bid.bid) {
+ build_id__init(&bid, sample->bid.bid->data, sample->bid.bid->size);
+ build_id__snprintf(&bid, sbuild_id, sizeof(sbuild_id));
+ }
+ printf("... build_id: %s offset: %#" PRIx64 "\n", sbuild_id, sample->bid.offset);
+ }
+
if (evsel__has_callchain(evsel))
callchain__printf(evsel, sample);
@@ -1751,7 +1851,7 @@ static void dump_deferred_callchain(union perf_event *event, struct perf_sample
printf("(IP, 0x%x): %d/%d: %#" PRIx64 "\n",
event->header.misc, sample->pid, sample->tid, sample->deferred_cookie);
- if (evsel__has_callchain(evsel))
+ if (evsel__has_callchain(evsel) || sample->callchain_bid.nr_offsets)
callchain__printf(evsel, sample);
}
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index 57d6efe9e5f5..288a2122101b 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -1717,6 +1717,15 @@ size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
result += sample->aux_sample.size;
}
+ if (type & PERF_SAMPLE_BUILD_ID_OFFSET)
+ result += sizeof(struct perf_build_id) + sizeof(u64);
+
+ if (type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) {
+ result += sizeof(u64) +
+ ALIGN(sample->callchain_bid.build_ids_len, sizeof(u64)) +
+ sample->callchain_bid.nr_offsets * sizeof(u64);
+ }
+
return result;
}
@@ -1973,6 +1982,38 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_fo
array = (void *)array + sz;
}
+ if (type & PERF_SAMPLE_BUILD_ID_OFFSET) {
+ if (sample->bid.bid) {
+ memcpy(array, sample->bid.bid, sizeof(struct perf_build_id));
+ array += sizeof(struct perf_build_id) / sizeof(u64);
+ *array++ = sample->bid.offset;
+ } else {
+ memset(array, 0, sizeof(struct perf_build_id));
+ array += sizeof(struct perf_build_id) / sizeof(u64);
+ *array++ = sample->ip;
+ }
+ }
+
+ if (type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) {
+ u32 aligned_bids_len =
+ ALIGN(sample->callchain_bid.build_ids_len, sizeof(u64));
+
+ u.val32[0] = sample->callchain_bid.nr_build_ids;
+ u.val32[1] = sample->callchain_bid.nr_offsets;
+ *array++ = u.val64;
+ if (aligned_bids_len && sample->callchain_bid.build_ids) {
+ memset(array, 0, aligned_bids_len);
+ memcpy(array, sample->callchain_bid.build_ids,
+ sample->callchain_bid.build_ids_len);
+ array += aligned_bids_len / sizeof(u64);
+ }
+ if (sample->callchain_bid.nr_offsets && sample->callchain_bid.offsets) {
+ sz = sample->callchain_bid.nr_offsets * sizeof(u64);
+ memcpy(array, sample->callchain_bid.offsets, sz);
+ array += sample->callchain_bid.nr_offsets;
+ }
+ }
+
return 0;
}
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 15/19] perf symbol: Resolve symbols and callchains using build ID and offset
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (13 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 14/19] perf evsel: Parse, synthesize, and dump BUILD_ID_OFFSET sample types Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 16/19] perf inject: Add --sample-buildids to convert samples to build ID + offset Ian Rogers
` (3 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Update thread__find_map(), callchain resolution in machine.c, deferred
callchain merging in sample__merge_deferred_callchain(), and perf
report/script to resolve symbols and callchains using the build ID and
file offset recorded in samples.
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-report.c | 6 +-
tools/perf/builtin-script.c | 27 +++--
tools/perf/util/callchain.c | 153 +++++++++++++++++++++++-----
tools/perf/util/event.c | 36 +++++--
tools/perf/util/evsel_fprintf.c | 9 +-
tools/perf/util/machine.c | 173 ++++++++++++++++++++++++--------
6 files changed, 317 insertions(+), 87 deletions(-)
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 6672a4d16529..4a04a2f6da70 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -384,7 +384,8 @@ static int report__setup_sample_type(struct report *rep)
session->itrace_synth_opts->add_last_branch)
sample_type |= PERF_SAMPLE_BRANCH_STACK;
- if (!is_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
+ if (!is_pipe && !(sample_type & (PERF_SAMPLE_CALLCHAIN |
+ PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET))) {
if (perf_hpp_list.parent) {
ui__error("Selected --sort parent, but no "
"callchain data. Did you call "
@@ -410,7 +411,8 @@ static int report__setup_sample_type(struct report *rep)
if (symbol_conf.cumulate_callchain) {
/* Silently ignore if callchain is missing */
- if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
+ if (!(sample_type & (PERF_SAMPLE_CALLCHAIN |
+ PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET))) {
symbol_conf.cumulate_callchain = false;
perf_hpp__cancel_cumulate(session->evlist);
}
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 376a4e0d7b0c..85b4cc86ee97 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -498,7 +498,8 @@ static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)
return -EINVAL;
if (PRINT_FIELD(IP)) {
- if (evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", PERF_OUTPUT_IP))
+ if (evsel__check_stype(evsel, PERF_SAMPLE_IP | PERF_SAMPLE_BUILD_ID_OFFSET,
+ "IP", PERF_OUTPUT_IP))
return -EINVAL;
}
@@ -515,7 +516,8 @@ static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)
return -EINVAL;
if (PRINT_FIELD(SYM) &&
- !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
+ !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP | PERF_SAMPLE_ADDR |
+ PERF_SAMPLE_BUILD_ID_OFFSET))) {
pr_err("Display of symbols requested but neither sample IP nor "
"sample address\navailable. Hence, no addresses to convert "
"to symbols.\n");
@@ -527,7 +529,8 @@ static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)
return -EINVAL;
}
if (PRINT_FIELD(DSO) &&
- !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
+ !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP | PERF_SAMPLE_ADDR |
+ PERF_SAMPLE_BUILD_ID_OFFSET))) {
pr_err("Display of DSO requested but no address to convert.\n");
return -EINVAL;
}
@@ -1796,7 +1799,8 @@ static int perf_sample__fprintf_bts(struct perf_sample *sample,
unsigned int print_opts = output[type].print_ip_opts;
struct callchain_cursor *cursor = NULL;
- if (symbol_conf.use_callchain && sample->callchain) {
+ if (symbol_conf.use_callchain &&
+ (sample->callchain || sample->callchain_bid.nr_offsets)) {
cursor = get_tls_callchain_cursor();
if (thread__resolve_callchain(al->thread, cursor,
sample, NULL, NULL,
@@ -2755,13 +2759,18 @@ static void process_event(struct perf_script *script,
fprintf(fp, " %s", cgrp_name);
}
- if (PRINT_FIELD(IP)) {
+ if (PRINT_FIELD(IP) ||
+ ((attr->sample_type & (PERF_SAMPLE_IP | PERF_SAMPLE_BUILD_ID_OFFSET)) &&
+ output[type].print_ip_opts &&
+ !(PRINT_FIELD(ADDR) || PRINT_FIELD(BRSTACK) ||
+ PRINT_FIELD(BRSTACKSYM) || PRINT_FIELD(BRSTACKOFF)))) {
struct callchain_cursor *cursor = NULL;
if (script->stitch_lbr)
thread__set_lbr_stitch_enable(al->thread, true);
- if (symbol_conf.use_callchain && sample->callchain) {
+ if (symbol_conf.use_callchain &&
+ (sample->callchain || sample->callchain_bid.nr_offsets)) {
cursor = get_tls_callchain_cursor();
if (thread__resolve_callchain(al->thread, cursor,
sample, NULL, NULL,
@@ -3005,10 +3014,11 @@ static int process_deferred_sample_event(const struct perf_tool *tool,
fprintf(fp, "DEFERRED CALLCHAIN [cookie: %llx]",
(unsigned long long)event->callchain_deferred.cookie);
- if (PRINT_FIELD(IP)) {
+ if (output[type].print_ip_opts) {
struct callchain_cursor *cursor = NULL;
- if (symbol_conf.use_callchain && sample->callchain) {
+ if (symbol_conf.use_callchain &&
+ (sample->callchain || sample->callchain_bid.nr_offsets)) {
cursor = get_tls_callchain_cursor();
if (thread__resolve_callchain(al.thread, cursor,
sample, NULL, NULL,
@@ -3090,6 +3100,7 @@ static int process_attr(const struct perf_tool *tool, union perf_event *event,
/* Enable fields for callchain entries */
if (symbol_conf.use_callchain &&
(sample_type & PERF_SAMPLE_CALLCHAIN ||
+ sample_type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET ||
sample_type & PERF_SAMPLE_BRANCH_STACK ||
(sample_type & PERF_SAMPLE_REGS_USER &&
sample_type & PERF_SAMPLE_STACK_USER))) {
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index 4e4696cfa6d4..57c6f940c2ad 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -1175,7 +1175,8 @@ int sample__resolve_callchain(struct perf_sample *sample,
struct addr_location *al,
int max_stack)
{
- if (sample->callchain == NULL && !symbol_conf.show_branchflag_count)
+ if (sample->callchain == NULL && sample->callchain_bid.nr_offsets == 0 &&
+ !symbol_conf.show_branchflag_count)
return 0;
if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
@@ -1188,7 +1189,8 @@ int sample__resolve_callchain(struct perf_sample *sample,
int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
{
- if ((!symbol_conf.use_callchain || sample->callchain == NULL) &&
+ if ((!symbol_conf.use_callchain ||
+ (sample->callchain == NULL && sample->callchain_bid.nr_offsets == 0)) &&
!symbol_conf.show_branchflag_count)
return 0;
return callchain_append(he->callchain, get_tls_callchain_cursor(), sample->period);
@@ -1914,32 +1916,137 @@ int sample__for_each_callchain_node(struct thread *thread,
int sample__merge_deferred_callchain(struct perf_sample *sample_orig,
struct perf_sample *sample_callchain)
{
- u64 nr_orig = sample_orig->callchain->nr - 1;
- u64 nr_deferred = sample_callchain->callchain->nr;
- struct ip_callchain *callchain;
+ if (sample_orig->deferred_bid && sample_callchain->callchain_bid.nr_offsets > 0) {
+ struct perf_build_id *bid =
+ (struct perf_build_id *)sample_orig->bid.deferred_storage;
+ u64 offset = 0;
+
+ perf_sample__callchain_bid_entry(sample_callchain, 0, bid, &offset);
+ sample_orig->bid.bid = bid;
+ sample_orig->bid.offset = offset;
+ if (!sample_orig->evsel ||
+ !(sample_orig->evsel->core.attr.sample_type & PERF_SAMPLE_IP))
+ sample_orig->ip = offset;
+ sample_orig->deferred_bid = false;
+ }
+
+ if (sample_orig->callchain_bid.nr_offsets >= 2 &&
+ sample_callchain->callchain_bid.nr_offsets > 0) {
+ u32 nr_orig = sample_orig->callchain_bid.nr_offsets - 1;
+ u32 nr_deferred = sample_callchain->callchain_bid.nr_offsets;
+
+ if (sample_orig->merged_callchain_bids)
+ return -EINVAL;
+
+ if (sample_orig->callchain_bid.offsets[nr_orig - 1] ==
+ PERF_CONTEXT_USER_DEFERRED) {
+ u32 max_orig_idx = 0, orig_bids_bytes = 0, def_bids_bytes = 0;
+ u32 total_bids, aligned_bids_len;
+ u32 total_offsets = nr_orig + nr_deferred;
+ u8 *buf;
+ u64 *offsets;
+
+ bool is_user_ctx = true;
+
+ for (u32 i = 0; i < nr_orig; i++) {
+ u64 raw = sample_orig->callchain_bid.offsets[i];
+ u8 idx;
+
+ if (raw >= PERF_CONTEXT_MAX) {
+ is_user_ctx = (raw == PERF_CONTEXT_USER ||
+ raw == PERF_CONTEXT_GUEST_USER ||
+ raw == PERF_CONTEXT_USER_DEFERRED);
+ continue;
+ }
+ if (!is_user_ctx)
+ continue;
+ idx = raw >> 56;
+ if (idx >= 1 && idx <= sample_orig->callchain_bid.nr_build_ids &&
+ idx > max_orig_idx)
+ max_orig_idx = idx;
+ }
+ for (u32 i = 0; i < max_orig_idx; i++) {
+ u8 sz = sample_orig->callchain_bid.build_ids[orig_bids_bytes];
- if (sample_orig->merged_callchain) {
- /* Already merged. */
- return -EINVAL;
- }
+ orig_bids_bytes += 1 + sz;
+ }
+ for (u32 i = 0; i < sample_callchain->callchain_bid.nr_build_ids; i++) {
+ u8 sz = sample_callchain->callchain_bid.build_ids[def_bids_bytes];
- if (sample_orig->callchain->nr < 2) {
- sample_orig->deferred_callchain = false;
- return -EINVAL;
+ def_bids_bytes += 1 + sz;
+ }
+
+ total_bids = max_orig_idx + sample_callchain->callchain_bid.nr_build_ids;
+ if (total_bids > 254)
+ return -EINVAL;
+ aligned_bids_len = ALIGN(orig_bids_bytes + def_bids_bytes, sizeof(u64));
+ buf = zalloc(aligned_bids_len + total_offsets * sizeof(u64));
+ if (!buf)
+ return -ENOMEM;
+
+ if (orig_bids_bytes)
+ memcpy(buf, sample_orig->callchain_bid.build_ids, orig_bids_bytes);
+ if (def_bids_bytes)
+ memcpy(buf + orig_bids_bytes,
+ sample_callchain->callchain_bid.build_ids, def_bids_bytes);
+
+ offsets = (u64 *)(buf + aligned_bids_len);
+ memcpy(offsets, sample_orig->callchain_bid.offsets,
+ nr_orig * sizeof(u64));
+ for (u32 i = 0; i < nr_deferred; i++) {
+ u64 raw = sample_callchain->callchain_bid.offsets[i];
+ u8 idx = raw >> 56;
+
+ if (idx >= 1 &&
+ idx <= sample_callchain->callchain_bid.nr_build_ids) {
+ u8 new_idx = idx + max_orig_idx;
+
+ raw = (raw & ((1ULL << 56) - 1)) | ((u64)new_idx << 56);
+ }
+ offsets[nr_orig + i] = raw;
+ }
+
+ if (sample_orig->allocated_callchain_bids)
+ free(sample_orig->callchain_bid.build_ids);
+ sample_orig->callchain_bid.nr_build_ids = total_bids;
+ sample_orig->callchain_bid.nr_offsets = total_offsets;
+ sample_orig->callchain_bid.build_ids_len = aligned_bids_len;
+ sample_orig->callchain_bid.build_ids = buf;
+ sample_orig->callchain_bid.offsets = offsets;
+ sample_orig->allocated_callchain_bids = true;
+ sample_orig->merged_callchain_bids = true;
+ }
}
- callchain = calloc(1 + nr_orig + nr_deferred, sizeof(u64));
- if (callchain == NULL)
- return -ENOMEM;
+ if (sample_orig->callchain && sample_callchain->callchain) {
+ u64 nr_orig = sample_orig->callchain->nr - 1;
+ u64 nr_deferred = sample_callchain->callchain->nr;
+ struct ip_callchain *callchain;
+
+ if (sample_orig->merged_callchain) {
+ /* Already merged. */
+ return -EINVAL;
+ }
+
+ if (sample_orig->callchain->nr < 2) {
+ sample_orig->deferred_callchain = false;
+ return -EINVAL;
+ }
+
+ callchain = calloc(1 + nr_orig + nr_deferred, sizeof(u64));
+ if (callchain == NULL)
+ return -ENOMEM;
- callchain->nr = nr_orig + nr_deferred;
- /* copy original including PERF_CONTEXT_USER_DEFERRED (but the cookie) */
- memcpy(callchain->ips, sample_orig->callchain->ips, nr_orig * sizeof(u64));
- /* copy deferred user callchains */
- memcpy(&callchain->ips[nr_orig], sample_callchain->callchain->ips,
- nr_deferred * sizeof(u64));
+ callchain->nr = nr_orig + nr_deferred;
+ /* copy original including PERF_CONTEXT_USER_DEFERRED (but the cookie) */
+ memcpy(callchain->ips, sample_orig->callchain->ips, nr_orig * sizeof(u64));
+ /* copy deferred user callchains */
+ memcpy(&callchain->ips[nr_orig], sample_callchain->callchain->ips,
+ nr_deferred * sizeof(u64));
+
+ sample_orig->merged_callchain = true;
+ sample_orig->callchain = callchain;
+ }
- sample_orig->merged_callchain = true;
- sample_orig->callchain = callchain;
return 0;
}
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index 4a6b59fa9d82..ddb572d33b24 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -702,8 +702,8 @@ struct map *thread__find_map(struct thread *thread, struct perf_sample *sample,
thread__zput(al->thread);
al->thread = thread__get(thread);
- al->addr = sample->ip;
al->cpumode = sample->cpumode;
+ al->addr = sample->bid.bid ? sample->bid.offset : sample->ip;
al->filtered = 0;
if (sample->cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
@@ -732,15 +732,31 @@ struct map *thread__find_map(struct thread *thread, struct perf_sample *sample,
return NULL;
}
- al->map = maps__find(maps, al->addr);
- if (al->map != NULL) {
- /*
- * Kernel maps might be changed when loading symbols so loading
- * must be done prior to using kernel maps.
- */
- if (load_map)
- map__load(al->map);
- al->addr = map__map_ip(al->map, al->addr);
+
+ if (sample->bid.bid && sample->bid.bid->size > 0) {
+ struct build_id bid;
+
+ build_id__init(&bid, sample->bid.bid->data, sample->bid.bid->size);
+ al->addr = sample->bid.offset;
+ al->map = maps__find_by_build_id(maps, &bid);
+ if (al->map != NULL) {
+ if (load_map)
+ map__load(al->map);
+ /* al->addr is already a file offset */
+ }
+ } else {
+ /* When bid.bid->size == 0, bid.ip holds the fallback virtual address. */
+ al->addr = sample->bid.bid ? sample->bid.ip : sample->ip;
+ al->map = maps__find(maps, al->addr);
+ if (al->map != NULL) {
+ /*
+ * Kernel maps might be changed when loading symbols so loading
+ * must be done prior to using kernel maps.
+ */
+ if (load_map)
+ map__load(al->map);
+ al->addr = map__map_ip(al->map, al->addr);
+ }
}
return al->map;
diff --git a/tools/perf/util/evsel_fprintf.c b/tools/perf/util/evsel_fprintf.c
index 0f7a25500a44..91f52ead2562 100644
--- a/tools/perf/util/evsel_fprintf.c
+++ b/tools/perf/util/evsel_fprintf.c
@@ -131,7 +131,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment,
if (cursor == NULL)
return fprintf(fp, "<not enough memory for the callchain cursor>%s", print_oneline ? "" : "\n");
- if (sample->callchain) {
+ if (sample->callchain || sample->callchain_bid.nr_offsets) {
callchain_cursor_commit(cursor);
while (1) {
@@ -232,8 +232,11 @@ int sample__fprintf_sym(struct perf_sample *sample, struct addr_location *al,
} else {
printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " ");
- if (print_ip)
- printed += fprintf(fp, "%16" PRIx64, sample->ip);
+ if (print_ip) {
+ u64 ip = sample->bid.bid ? sample->bid.offset : sample->ip;
+
+ printed += fprintf(fp, "%16" PRIx64, ip);
+ }
if (print_sym) {
printed += fprintf(fp, " ");
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index a228361a8490..417edc37c487 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -2250,7 +2250,9 @@ static int add_callchain_ip(struct thread *thread,
struct branch_flags *flags,
struct iterations *iter,
u64 branch_from,
- bool symbols)
+ bool symbols,
+ struct perf_build_id *bid,
+ u64 offset)
{
static __thread struct perf_sample sample;
struct map_symbol ms = {};
@@ -2264,6 +2266,8 @@ static int add_callchain_ip(struct thread *thread,
al.sym = NULL;
al.srcline = NULL;
sample.ip = ip;
+ sample.bid.bid = bid;
+ sample.bid.offset = offset;
if (!cpumode) {
thread__find_cpumode_addr_location(thread, &sample, symbols, &al);
} else {
@@ -2328,6 +2332,9 @@ static int add_callchain_ip(struct thread *thread,
ms.map = map__get(al.map);
ms.sym = al.sym;
+ if (bid && bid->size > 0 && al.map)
+ ip = map__unmap_ip(al.map, al.addr);
+
if (append_inlines(cursor, &ms, ip, branch, flags, nr_loop_iter,
iter_cycles, branch_from) == 0)
goto out;
@@ -2443,10 +2450,18 @@ static int lbr_callchain_add_kernel_ip(struct thread *thread,
if (callee) {
for (i = 0; i < end + 1; i++) {
+ struct perf_build_id bid_buf;
+ struct perf_build_id *bid = NULL;
+ u64 offset = 0;
+
+ if (i < (int)sample->callchain_bid.nr_offsets) {
+ perf_sample__callchain_bid_entry(sample, i, &bid_buf, &offset);
+ bid = &bid_buf;
+ }
err = add_callchain_ip(thread, cursor, parent,
root_al, &cpumode, chain->ips[i],
false, NULL, NULL, branch_from,
- symbols);
+ symbols, bid, offset);
if (err)
return err;
}
@@ -2454,10 +2469,18 @@ static int lbr_callchain_add_kernel_ip(struct thread *thread,
}
for (i = end; i >= 0; i--) {
+ struct perf_build_id bid_buf;
+ struct perf_build_id *bid = NULL;
+ u64 offset = 0;
+
+ if (i < (int)sample->callchain_bid.nr_offsets) {
+ perf_sample__callchain_bid_entry(sample, i, &bid_buf, &offset);
+ bid = &bid_buf;
+ }
err = add_callchain_ip(thread, cursor, parent,
root_al, &cpumode, chain->ips[i],
false, NULL, NULL, branch_from,
- symbols);
+ symbols, bid, offset);
if (err)
return err;
}
@@ -2531,16 +2554,25 @@ static int lbr_callchain_add_lbr_ip(struct thread *thread,
* Set the (first) leaf function's IP to sample->ip (the
* location of the sample) but if not recorded use entries.to
*/
- if (sample->ip)
+ if ((sample->bid.bid && sample->bid.bid->size > 0) || sample->ip) {
ip = sample->ip;
- else
+ flags = &entries[0].flags;
+ *branch_from = entries[0].from;
+ err = add_callchain_ip(thread, cursor, parent,
+ root_al, &cpumode, ip,
+ true, flags, NULL,
+ *branch_from, symbols,
+ sample->bid.bid, sample->bid.offset);
+ } else {
ip = entries[0].to;
- flags = &entries[0].flags;
- *branch_from = entries[0].from;
- err = add_callchain_ip(thread, cursor, parent,
- root_al, &cpumode, ip,
- true, flags, NULL,
- *branch_from, symbols);
+ flags = &entries[0].flags;
+ *branch_from = entries[0].from;
+ err = add_callchain_ip(thread, cursor, parent,
+ root_al, &cpumode, ip,
+ true, flags, NULL,
+ *branch_from, symbols,
+ /*bid=*/NULL, /*offset=*/0);
+ }
if (err)
return err;
@@ -2565,7 +2597,7 @@ static int lbr_callchain_add_lbr_ip(struct thread *thread,
err = add_callchain_ip(thread, cursor, parent,
root_al, &cpumode, ip,
true, flags, NULL,
- *branch_from, symbols);
+ *branch_from, symbols, /*bid=*/NULL, /*offset=*/0);
if (err)
return err;
save_lbr_cursor_node(thread, cursor, i);
@@ -2580,7 +2612,7 @@ static int lbr_callchain_add_lbr_ip(struct thread *thread,
err = add_callchain_ip(thread, cursor, parent,
root_al, &cpumode, ip,
true, flags, NULL,
- *branch_from, symbols);
+ *branch_from, symbols, /*bid=*/NULL, /*offset=*/0);
if (err)
return err;
save_lbr_cursor_node(thread, cursor, i);
@@ -2591,16 +2623,25 @@ static int lbr_callchain_add_lbr_ip(struct thread *thread,
* Set the (first) leaf function's IP to sample->ip (the
* location of the sample) but if not recorded use entries.to
*/
- if (sample->ip)
+ if ((sample->bid.bid && sample->bid.bid->size > 0) || sample->ip) {
ip = sample->ip;
- else
+ flags = &entries[0].flags;
+ *branch_from = entries[0].from;
+ err = add_callchain_ip(thread, cursor, parent,
+ root_al, &cpumode, ip,
+ true, flags, NULL,
+ *branch_from, symbols,
+ sample->bid.bid, sample->bid.offset);
+ } else {
ip = entries[0].to;
- flags = &entries[0].flags;
- *branch_from = entries[0].from;
- err = add_callchain_ip(thread, cursor, parent,
- root_al, &cpumode, ip,
- true, flags, NULL,
- *branch_from, symbols);
+ flags = &entries[0].flags;
+ *branch_from = entries[0].from;
+ err = add_callchain_ip(thread, cursor, parent,
+ root_al, &cpumode, ip,
+ true, flags, NULL,
+ *branch_from, symbols,
+ /*bid=*/NULL, /*offset=*/0);
+ }
if (err)
return err;
}
@@ -2773,12 +2814,16 @@ static int resolve_lbr_callchain_sample(struct thread *thread,
{
bool callee = (callchain_param.order == ORDER_CALLEE);
struct ip_callchain *chain = sample->callchain;
- int chain_nr = min(max_stack, (int)chain->nr), i;
+ int chain_nr, i;
struct lbr_stitch *lbr_stitch;
bool stitched_lbr = false;
u64 branch_from = 0;
int err;
+ if (!chain)
+ return 0;
+
+ chain_nr = min_t(int, max_stack, chain->nr);
for (i = 0; i < chain_nr; i++) {
if (chain->ips[i] == PERF_CONTEXT_USER)
break;
@@ -2864,7 +2909,8 @@ static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
if (ip >= PERF_CONTEXT_MAX) {
err = add_callchain_ip(thread, cursor, parent,
root_al, cpumode, ip,
- false, NULL, NULL, 0, symbols);
+ false, NULL, NULL, 0, symbols,
+ /*bid=*/NULL, /*offset=*/0);
break;
}
}
@@ -2892,6 +2938,7 @@ static int thread__resolve_callchain_sample(struct thread *thread,
struct branch_stack *branch = sample->branch_stack;
struct branch_entry *entries = perf_sample__branch_entries(sample);
struct ip_callchain *chain = sample->callchain;
+ struct ip_callchain *alloc_chain = NULL;
int chain_nr = 0;
u8 cpumode = PERF_RECORD_MISC_USER;
int i, j, err, nr_entries, usr_idx;
@@ -2899,6 +2946,22 @@ static int thread__resolve_callchain_sample(struct thread *thread,
int first_call = 0;
u64 leaf_frame_caller;
+ if (!chain && sample->callchain_bid.nr_offsets > 0) {
+ alloc_chain = malloc(sizeof(*chain) +
+ sample->callchain_bid.nr_offsets * sizeof(u64));
+ if (!alloc_chain)
+ return -ENOMEM;
+ alloc_chain->nr = sample->callchain_bid.nr_offsets;
+ for (i = 0; i < (int)sample->callchain_bid.nr_offsets; i++) {
+ u64 offset = 0;
+
+ perf_sample__callchain_bid_entry(sample, i, NULL, &offset);
+ alloc_chain->ips[i] = offset;
+ }
+ chain = alloc_chain;
+ sample->callchain = alloc_chain;
+ }
+
if (chain)
chain_nr = chain->nr;
@@ -2909,8 +2972,10 @@ static int thread__resolve_callchain_sample(struct thread *thread,
root_al, max_stack,
!env ? 0 : env->max_branches,
symbols);
- if (err)
- return (err < 0) ? err : 0;
+ if (err) {
+ err = (err < 0) ? err : 0;
+ goto out;
+ }
}
/*
@@ -2973,22 +3038,26 @@ static int thread__resolve_callchain_sample(struct thread *thread,
root_al,
NULL, be[i].to,
true, &be[i].flags,
- NULL, be[i].from, symbols);
+ NULL, be[i].from, symbols,
+ /*bid=*/NULL, /*offset=*/0);
if (!err) {
err = add_callchain_ip(thread, cursor, parent, root_al,
NULL, be[i].from,
true, &be[i].flags,
- &iter[i], 0, symbols);
+ &iter[i], 0, symbols,
+ /*bid=*/NULL, /*offset=*/0);
}
if (err == -EINVAL)
break;
if (err)
- return err;
+ goto out;
}
- if (chain_nr == 0)
- return 0;
+ if (chain_nr == 0) {
+ err = 0;
+ goto out;
+ }
chain_nr -= nr;
}
@@ -2997,12 +3066,17 @@ static int thread__resolve_callchain_sample(struct thread *thread,
if (chain && callchain_param.order != ORDER_CALLEE) {
err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
&cpumode, chain->nr - first_call, symbols);
- if (err)
- return (err < 0) ? err : 0;
+ if (err) {
+ err = (err < 0) ? err : 0;
+ goto out;
+ }
}
for (i = first_call, nr_entries = 0;
i < chain_nr && nr_entries < max_stack; i++) {
u64 ip;
+ struct perf_build_id bid_buf;
+ struct perf_build_id *bid = NULL;
+ u64 offset = 0;
if (callchain_param.order == ORDER_CALLEE)
j = i;
@@ -3014,13 +3088,19 @@ static int thread__resolve_callchain_sample(struct thread *thread,
continue;
#endif
ip = chain->ips[j];
+ if (j < (int)sample->callchain_bid.nr_offsets) {
+ perf_sample__callchain_bid_entry(sample, j, &bid_buf, &offset);
+ bid = &bid_buf;
+ }
if (ip < PERF_CONTEXT_MAX)
++nr_entries;
else if (callchain_param.order != ORDER_CALLEE) {
err = find_prev_cpumode(chain, thread, cursor, parent,
root_al, &cpumode, j, symbols);
- if (err)
- return (err < 0) ? err : 0;
+ if (err) {
+ err = (err < 0) ? err : 0;
+ goto out;
+ }
continue;
}
@@ -3046,21 +3126,32 @@ static int thread__resolve_callchain_sample(struct thread *thread,
err = add_callchain_ip(thread, cursor, parent,
root_al, &cpumode, leaf_frame_caller,
- false, NULL, NULL, 0, symbols);
- if (err)
- return (err < 0) ? err : 0;
+ false, NULL, NULL, 0, symbols,
+ /*bid=*/NULL, /*offset=*/0);
+ if (err) {
+ err = (err < 0) ? err : 0;
+ goto out;
+ }
}
}
err = add_callchain_ip(thread, cursor, parent,
root_al, &cpumode, ip,
- false, NULL, NULL, 0, symbols);
+ false, NULL, NULL, 0, symbols, bid, offset);
- if (err)
- return (err < 0) ? err : 0;
+ if (err) {
+ err = (err < 0) ? err : 0;
+ goto out;
+ }
}
- return 0;
+ err = 0;
+out:
+ if (alloc_chain) {
+ sample->callchain = NULL;
+ free(alloc_chain);
+ }
+ return err;
}
static int unwind_entry(struct unwind_entry *entry, void *arg)
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 16/19] perf inject: Add --sample-buildids to convert samples to build ID + offset
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (14 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 15/19] perf symbol: Resolve symbols and callchains using build ID and offset Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 17/19] perf record: Add --buildid-offset option Ian Rogers
` (2 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Add the --sample-buildids (-S) option to perf inject, allowing it to
drop user-space MMAP events and rewrite PERF_SAMPLE_IP,
PERF_SAMPLE_CALLCHAIN, and PERF_RECORD_CALLCHAIN_DEFERRED records to use
build IDs and file offsets instead of virtual addresses.
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/Documentation/perf-inject.txt | 8 +
tools/perf/builtin-inject.c | 71 +-
tools/perf/util/Build | 1 +
tools/perf/util/inject_bid_offset.c | 882 +++++++++++++++++++++++
tools/perf/util/inject_bid_offset.h | 15 +
5 files changed, 970 insertions(+), 7 deletions(-)
create mode 100644 tools/perf/util/inject_bid_offset.c
create mode 100644 tools/perf/util/inject_bid_offset.h
diff --git a/tools/perf/Documentation/perf-inject.txt b/tools/perf/Documentation/perf-inject.txt
index 95dfdf39666e..297247b643d8 100644
--- a/tools/perf/Documentation/perf-inject.txt
+++ b/tools/perf/Documentation/perf-inject.txt
@@ -32,6 +32,14 @@ OPTIONS
Inject build-ids of all DSOs into the output stream regardless of hits
and skip SAMPLE processing.
+-S::
+--sample-buildids::
+ Drop user-space MMAP and MMAP2 events, inject build-id records for DSOs
+ hit by samples, and rewrite PERF_SAMPLE_IP, PERF_SAMPLE_CALLCHAIN, and
+ PERF_RECORD_CALLCHAIN_DEFERRED records to use build IDs and DSO file
+ offsets instead of virtual addresses. Mutually exclusive with --aslr
+ and --convert-callchain.
+
--known-build-ids=::
Override build-ids to inject using these comma-separated pairs of
build-id and path. Understands file://filename to read these pairs
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index f59f3005899c..2309074b2f65 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -9,6 +9,7 @@
#include "builtin.h"
#include "util/aslr.h"
+#include "util/inject_bid_offset.h"
#include "util/color.h"
#include "util/dso.h"
#include "util/vdso.h"
@@ -46,6 +47,7 @@
#include <errno.h>
#include <signal.h>
#include <inttypes.h>
+#include <stdlib.h>
struct guest_event {
struct perf_sample sample;
@@ -112,6 +114,7 @@ enum build_id_rewrite_style {
BID_RWS__INJECT_HEADER_ALL,
BID_RWS__MMAP2_BUILDID_ALL,
BID_RWS__MMAP2_BUILDID_LAZY,
+ BID_RWS__SAMPLE_BUILDID,
};
struct perf_inject {
@@ -243,12 +246,18 @@ static int perf_event__repipe_attr(const struct perf_tool *tool,
if (ret)
return ret;
- if (inject->aslr) {
+ if (inject->aslr || inject->build_id_style == BID_RWS__SAMPLE_BUILDID) {
aslr_event = malloc(event->header.size);
if (!aslr_event)
return -ENOMEM;
memcpy(aslr_event, event, event->header.size);
- aslr_tool__strip_attr_event(aslr_event, *pevlist);
+ if (inject->aslr)
+ aslr_tool__strip_attr_event(aslr_event, *pevlist);
+ if (inject->build_id_style == BID_RWS__SAMPLE_BUILDID) {
+ ret = perf_event__rewrite_attr_for_build_id_offset(&aslr_event->attr.attr);
+ if (ret)
+ goto out;
+ }
event = aslr_event;
}
@@ -2565,7 +2574,8 @@ static int __cmd_inject(struct perf_inject *inject)
};
if (inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
- inject->build_id_style == BID_RWS__INJECT_HEADER_ALL)
+ inject->build_id_style == BID_RWS__INJECT_HEADER_ALL ||
+ inject->build_id_style == BID_RWS__SAMPLE_BUILDID)
perf_header__set_feat(&session->header, HEADER_BUILD_ID);
/*
* Keep all buildids when there is unprocessed AUX data because
@@ -2620,6 +2630,17 @@ static int __cmd_inject(struct perf_inject *inject)
if (inject->aslr)
aslr_tool__strip_evlist(inject->session->tool, session->evlist);
+ if (inject->build_id_style == BID_RWS__SAMPLE_BUILDID) {
+ struct evsel *evsel;
+
+ evlist__for_each_entry(session->evlist, evsel) {
+ struct perf_event_attr *attr = &evsel->core.attr;
+
+ ret = perf_event__rewrite_attr_for_build_id_offset(attr);
+ if (ret)
+ return ret;
+ }
+ }
session->header.data_offset = output_data_offset;
session->header.data_size = inject->bytes_written;
@@ -2678,6 +2699,7 @@ int cmd_inject(int argc, const char **argv)
bool build_id_all = false;
bool mmap2_build_ids = false;
bool mmap2_build_id_all = false;
+ bool build_id_sample = false;
struct option options[] = {
OPT_BOOLEAN('b', "build-ids", &build_ids,
@@ -2686,8 +2708,11 @@ int cmd_inject(int argc, const char **argv)
"Inject build-ids of all DSOs into the output stream"),
OPT_BOOLEAN('B', "mmap2-buildids", &mmap2_build_ids,
"Drop unused mmap events, make others mmap2 with build IDs"),
+
OPT_BOOLEAN(0, "mmap2-buildid-all", &mmap2_build_id_all,
"Rewrite all mmap events as mmap2 events with build IDs"),
+ OPT_BOOLEAN('S', "sample-buildids", &build_id_sample,
+ "Drop all mmap events and rewrite samples to use build ID + offset"),
OPT_STRING(0, "known-build-ids", &known_build_ids,
"buildid path [,buildid path...]",
"build-ids to use for given paths"),
@@ -2760,8 +2785,8 @@ int cmd_inject(int argc, const char **argv)
if (argc)
usage_with_options(inject_usage, options);
- if (inject.aslr && inject.convert_callchain) {
- pr_err("Error: --aslr and --convert-callchain are mutually exclusive features.\n");
+ if ((inject.aslr + inject.convert_callchain + build_id_sample) > 1) {
+ pr_err("Error: --aslr, --convert-callchain and --sample-buildids are mutually exclusive features.\n");
return -EINVAL;
}
@@ -2806,8 +2831,11 @@ int cmd_inject(int argc, const char **argv)
inject.build_id_style = BID_RWS__MMAP2_BUILDID_ALL;
if (build_ids)
inject.build_id_style = BID_RWS__INJECT_HEADER_LAZY;
+
if (build_id_all)
inject.build_id_style = BID_RWS__INJECT_HEADER_ALL;
+ if (build_id_sample)
+ inject.build_id_style = BID_RWS__SAMPLE_BUILDID;
data.path = inject.input_name;
@@ -2817,7 +2845,8 @@ int cmd_inject(int argc, const char **argv)
inject.guest_session.perf_data_file ||
inject.convert_callchain ||
inject.build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
- inject.build_id_style == BID_RWS__MMAP2_BUILDID_LAZY;
+ inject.build_id_style == BID_RWS__MMAP2_BUILDID_LAZY ||
+ inject.build_id_style == BID_RWS__SAMPLE_BUILDID;
perf_tool__init(&inject.tool, ordered_events);
inject.tool.sample = perf_event__repipe_sample;
inject.tool.read = perf_event__repipe_sample;
@@ -2839,6 +2868,7 @@ int cmd_inject(int argc, const char **argv)
inject.tool.ksymbol = perf_event__repipe;
inject.tool.bpf = perf_event__repipe;
inject.tool.text_poke = perf_event__repipe;
+ inject.tool.callchain_deferred = perf_event__repipe;
inject.tool.attr = perf_event__repipe_attr;
inject.tool.event_update = perf_event__repipe_event_update;
inject.tool.tracing_data = perf_event__repipe_op2_synth;
@@ -2868,7 +2898,8 @@ int cmd_inject(int argc, const char **argv)
inject.guest_session.perf_data_file ||
inject.convert_callchain ||
inject.build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
- inject.build_id_style == BID_RWS__MMAP2_BUILDID_LAZY) {
+ inject.build_id_style == BID_RWS__MMAP2_BUILDID_LAZY ||
+ inject.build_id_style == BID_RWS__SAMPLE_BUILDID) {
/*
* to make sure the mmap records are ordered correctly
* and so that the correct especially due to jitted code
@@ -2883,6 +2914,12 @@ int cmd_inject(int argc, const char **argv)
ret = -ENOMEM;
goto out_close_output;
}
+ } else if (inject.build_id_style == BID_RWS__SAMPLE_BUILDID) {
+ tool = inject_bid_offset_tool__new(&inject.tool);
+ if (!tool) {
+ ret = -ENOMEM;
+ goto out_close_output;
+ }
}
inject.session = __perf_session__new(&data, tool,
/*trace_event_repipe=*/inject.output.is_pipe,
@@ -2890,8 +2927,11 @@ int cmd_inject(int argc, const char **argv)
if (IS_ERR(inject.session)) {
ret = PTR_ERR(inject.session);
+
if (inject.aslr)
aslr_tool__delete(tool);
+ if (inject.build_id_style == BID_RWS__SAMPLE_BUILDID)
+ inject_bid_offset_tool__delete(tool);
goto out_close_output;
}
@@ -2931,8 +2971,19 @@ int cmd_inject(int argc, const char **argv)
* the input.
*/
if (!data.is_pipe) {
+ struct evsel *evsel;
+
if (inject.aslr)
aslr_tool__strip_evlist(tool, inject.session->evlist);
+ if (inject.build_id_style == BID_RWS__SAMPLE_BUILDID) {
+ evlist__for_each_entry(inject.session->evlist, evsel) {
+ struct perf_event_attr *attr = &evsel->core.attr;
+
+ ret = perf_event__rewrite_attr_for_build_id_offset(attr);
+ if (ret)
+ goto out_delete;
+ }
+ }
ret = perf_event__synthesize_for_pipe(&inject.tool,
inject.session,
@@ -2941,6 +2992,10 @@ int cmd_inject(int argc, const char **argv)
if (inject.aslr)
aslr_tool__restore_evlist(tool, inject.session->evlist);
+ if (inject.build_id_style == BID_RWS__SAMPLE_BUILDID) {
+ evlist__for_each_entry(inject.session->evlist, evsel)
+ perf_event__rewrite_attr_for_sample_ip(&evsel->core.attr);
+ }
if (ret < 0)
goto out_delete;
@@ -3004,6 +3059,8 @@ int cmd_inject(int argc, const char **argv)
perf_session__delete(inject.session);
if (inject.aslr)
aslr_tool__delete(tool);
+ if (inject.build_id_style == BID_RWS__SAMPLE_BUILDID)
+ inject_bid_offset_tool__delete(tool);
out_close_output:
if (!inject.in_place_update)
perf_data__close(&inject.output);
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 2c1f880c4c47..25713e147016 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -7,6 +7,7 @@ perf-util-y += addr2line.o
perf-util-y += addr_location.o
perf-util-y += annotate.o
perf-util-y += aslr.o
+perf-util-y += inject_bid_offset.o
perf-util-y += blake2s.o
perf-util-y += block-info.o
perf-util-y += block-range.o
diff --git a/tools/perf/util/inject_bid_offset.c b/tools/perf/util/inject_bid_offset.c
new file mode 100644
index 000000000000..e27b99743ecc
--- /dev/null
+++ b/tools/perf/util/inject_bid_offset.c
@@ -0,0 +1,882 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "inject_bid_offset.h"
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <linux/compiler.h>
+#include <linux/kernel.h>
+#include <linux/overflow.h>
+#include <linux/string.h>
+#include <linux/zalloc.h>
+
+#include "addr_location.h"
+#include "debug.h"
+#include "dso.h"
+#include "event.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "hashmap.h"
+#include "header.h"
+#include "machine.h"
+#include "map.h"
+#include "namespaces.h"
+#include "sample.h"
+#include "session.h"
+#include "symbol.h"
+#include "synthetic-events.h"
+#include "thread.h"
+#include "tool.h"
+#include "vdso.h"
+
+static size_t dso_ptr_hash(long key, void *ctx __maybe_unused)
+{
+ return (size_t)key;
+}
+
+static bool dso_ptr_equal(long key1, long key2, void *ctx __maybe_unused)
+{
+ return key1 == key2;
+}
+
+struct inject_bid_offset_tool {
+ struct delegate_tool tool;
+ struct hashmap synthesized_dsos;
+ char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8);
+};
+
+int perf_event__rewrite_attr_for_build_id_offset(struct perf_event_attr *attr)
+{
+ if (attr->sample_type & (PERF_SAMPLE_BUILD_ID_OFFSET |
+ PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET)) {
+ /*
+ * Expect to add build ID information from virtual address, if
+ * it is already present then things would be confused so fail.
+ */
+ return -1;
+ }
+ if (attr->sample_type & PERF_SAMPLE_IP) {
+ attr->sample_type &= ~PERF_SAMPLE_IP;
+ attr->sample_type |= PERF_SAMPLE_BUILD_ID_OFFSET;
+ }
+ if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
+ attr->sample_type &= ~PERF_SAMPLE_CALLCHAIN;
+ attr->sample_type |= PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET;
+ }
+ return 0;
+}
+
+int perf_event__rewrite_attr_for_sample_ip(struct perf_event_attr *attr)
+{
+ if (attr->sample_type & (PERF_SAMPLE_IP | PERF_SAMPLE_CALLCHAIN)) {
+ /*
+ * Expect to remove build ID information for virtual address, if
+ * it is already present then things would be confused so fail.
+ */
+ return -1;
+ }
+ if (attr->sample_type & PERF_SAMPLE_BUILD_ID_OFFSET) {
+ attr->sample_type &= ~PERF_SAMPLE_BUILD_ID_OFFSET;
+ attr->sample_type |= PERF_SAMPLE_IP;
+ }
+ if (attr->sample_type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) {
+ attr->sample_type &= ~PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET;
+ attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
+ }
+ return 0;
+}
+
+static int dso__read_build_id(struct dso *dso)
+{
+ struct nscookie nsc;
+ struct build_id bid = { .size = 0, };
+
+ if (dso__has_build_id(dso))
+ return 0;
+
+ mutex_lock(dso__lock(dso));
+ nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
+ if (filename__read_build_id(dso__long_name(dso), &bid) > 0)
+ dso__set_build_id(dso, &bid);
+ else if (dso__nsinfo(dso)) {
+ char *new_name = dso__filename_with_chroot(dso, dso__long_name(dso));
+
+ if (new_name && filename__read_build_id(new_name, &bid) > 0)
+ dso__set_build_id(dso, &bid);
+ free(new_name);
+ }
+ nsinfo__mountns_exit(&nsc);
+ mutex_unlock(dso__lock(dso));
+
+ return dso__has_build_id(dso) ? 0 : -1;
+}
+
+static void perf_event__inject_sample_buildid_array(struct thread *thread,
+ u64 ip, u8 cpumode,
+ __u64 *array)
+{
+ struct perf_build_id bid = { .size = 0 };
+ u64 offset = ip;
+ struct addr_location al;
+ struct dso *dso;
+ const struct build_id *dso_bid;
+
+ struct perf_sample ps = { .ip = ip, .cpumode = cpumode };
+
+ addr_location__init(&al);
+
+ if (!thread ||
+ (cpumode != PERF_RECORD_MISC_USER &&
+ cpumode != PERF_RECORD_MISC_GUEST_USER))
+ goto write_bid;
+
+ if (!thread__find_map(thread, &ps, &al))
+ goto write_bid;
+
+ dso = al.map ? dso__get(map__dso(al.map)) : NULL;
+ if (!dso)
+ goto write_bid;
+ if (!dso__has_build_id(dso))
+ dso__read_build_id(dso);
+ dso_bid = dso__bid(dso);
+ if (!dso_bid || dso_bid->size == 0 ||
+ map__mapping_type(al.map) != MAPPING_TYPE__DSO) {
+ dso__put(dso);
+ goto write_bid;
+ }
+
+ bid.size = dso_bid->size;
+ if (bid.size > sizeof(bid.data))
+ bid.size = sizeof(bid.data);
+ memcpy(bid.data, &dso_bid->data, bid.size);
+ offset = map__dso_map_ip(al.map, offset);
+ dso__put(dso);
+
+write_bid:
+ compiletime_assert(sizeof(struct perf_build_id) == 3 * sizeof(u64),
+ "Unexpected perf_build_id size");
+ memcpy(&array[0], &bid, 3 * sizeof(u64));
+ array[3] = offset;
+ addr_location__exit(&al);
+}
+
+static size_t perf_event__inject_callchain_buildid_array(struct thread *thread,
+ const struct ip_callchain *callchain,
+ __u64 *out_array,
+ size_t max_words,
+ int *errp)
+{
+ u32 nr_offsets = callchain->nr;
+ u32 nr_build_ids = 0;
+ u32 build_ids_bytes = 0, aligned_bids_len;
+ size_t total_words;
+ u8 cpumode = PERF_RECORD_MISC_USER;
+ u8 build_ids[254 * 21];
+ u32 bid_offsets[254];
+ u64 *offsets;
+ union {
+ u64 val64;
+ u32 val32[2];
+ } u;
+
+ offsets = calloc(nr_offsets, sizeof(u64));
+ if (!offsets && nr_offsets > 0) {
+ *errp = -ENOMEM;
+ return 0;
+ }
+
+ for (u32 x = 0; x < nr_offsets; x++) {
+ u64 ip = callchain->ips[x];
+ __u64 entry_words[4];
+ struct perf_build_id *bid = (struct perf_build_id *)&entry_words[0];
+ u64 offset;
+ u8 bid_idx = 0;
+
+ if (ip >= PERF_CONTEXT_MAX) {
+ switch (ip) {
+ case PERF_CONTEXT_HV:
+ cpumode = PERF_RECORD_MISC_HYPERVISOR;
+ break;
+ case PERF_CONTEXT_KERNEL:
+ cpumode = PERF_RECORD_MISC_KERNEL;
+ break;
+ case PERF_CONTEXT_USER:
+ cpumode = PERF_RECORD_MISC_USER;
+ break;
+ case PERF_CONTEXT_GUEST:
+ case PERF_CONTEXT_GUEST_KERNEL:
+ cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
+ break;
+ case PERF_CONTEXT_GUEST_USER:
+ cpumode = PERF_RECORD_MISC_GUEST_USER;
+ break;
+ case PERF_CONTEXT_USER_DEFERRED:
+ cpumode = PERF_RECORD_MISC_USER;
+ offsets[x] = ip;
+ if (x + 1 < nr_offsets) {
+ x++;
+ offsets[x] = callchain->ips[x];
+ }
+ continue;
+ default:
+ break;
+ }
+ offsets[x] = ip;
+ continue;
+ }
+
+ perf_event__inject_sample_buildid_array(thread, ip, cpumode, entry_words);
+ offset = entry_words[3];
+ if (bid->size > 0 && bid->size <= 20 && (offset >> 56) == 0) {
+ for (u32 j = 0; j < nr_build_ids; j++) {
+ u32 pos = bid_offsets[j];
+
+ if (build_ids[pos] == bid->size &&
+ !memcmp(&build_ids[pos + 1], bid->data, bid->size)) {
+ bid_idx = j + 1;
+ break;
+ }
+ }
+ if (!bid_idx && nr_build_ids < 254) {
+ bid_offsets[nr_build_ids] = build_ids_bytes;
+ build_ids[build_ids_bytes++] = bid->size;
+ memcpy(&build_ids[build_ids_bytes], bid->data, bid->size);
+ build_ids_bytes += bid->size;
+ nr_build_ids++;
+ bid_idx = nr_build_ids;
+ }
+ }
+ if (bid_idx)
+ offsets[x] = (offset & ((1ULL << 56) - 1)) | ((u64)bid_idx << 56);
+ else if (cpumode == PERF_RECORD_MISC_USER ||
+ cpumode == PERF_RECORD_MISC_GUEST_USER)
+ offsets[x] = ip & ((1ULL << 56) - 1);
+ else
+ offsets[x] = ip;
+ }
+
+ aligned_bids_len = ALIGN(build_ids_bytes, sizeof(u64));
+ total_words = 1 + aligned_bids_len / sizeof(u64) + nr_offsets;
+ if (total_words > max_words) {
+ free(offsets);
+ *errp = -E2BIG;
+ return 0;
+ }
+ u.val32[0] = nr_build_ids;
+ u.val32[1] = nr_offsets;
+ out_array[0] = u.val64;
+ if (aligned_bids_len > 0) {
+ memset(&out_array[1], 0, aligned_bids_len);
+ memcpy(&out_array[1], build_ids, build_ids_bytes);
+ }
+ if (nr_offsets > 0) {
+ memcpy(&out_array[1 + aligned_bids_len / sizeof(u64)],
+ offsets, nr_offsets * sizeof(u64));
+ }
+ free(offsets);
+ return total_words;
+}
+
+static int inject_bid_offset_tool__process_build_id(const struct perf_tool *tool,
+ union perf_event *event,
+ struct perf_sample *sample __maybe_unused,
+ struct machine *machine __maybe_unused)
+{
+ return tool->build_id(tool, NULL, event);
+}
+
+static bool dso__is_in_kernel_space(struct dso *dso)
+{
+ if (dso__is_vdso(dso))
+ return false;
+
+ return dso__is_kcore(dso) ||
+ dso__kernel(dso) ||
+ is_kernel_module(dso__long_name(dso), PERF_RECORD_MISC_CPUMODE_UNKNOWN);
+}
+
+static void mark_dso_hit(struct inject_bid_offset_tool *ibo,
+ struct perf_sample *sample, struct machine *machine,
+ struct thread *thread, u64 ip, u8 cpumode)
+{
+ struct perf_sample ps = { .ip = ip, .cpumode = cpumode };
+ const struct perf_tool *tool = ibo->tool.delegate;
+ const struct build_id *bid;
+ struct addr_location al;
+ struct dso *dso;
+
+ addr_location__init(&al);
+ if (!thread__find_map(thread, &ps, &al))
+ goto out;
+
+ dso = al.map ? dso__get(map__dso(al.map)) : NULL;
+ if (!dso)
+ goto out;
+
+ if (!hashmap__find(&ibo->synthesized_dsos, RC_CHK_ACCESS(dso), NULL)) {
+ struct dso *saved_dso = dso__get(dso);
+
+ if (hashmap__add(&ibo->synthesized_dsos, RC_CHK_ACCESS(dso),
+ saved_dso))
+ dso__put(saved_dso);
+ dso__set_hit(dso);
+ if (!dso__has_build_id(dso))
+ dso__read_build_id(dso);
+ bid = dso__bid(dso);
+ if (bid && bid->size > 0) {
+ bool in_kernel = dso__is_in_kernel_space(dso);
+ const char *name;
+ __u16 misc;
+
+ if (dso__is_vdso(dso))
+ name = dso__short_name(dso);
+ else if (dso__is_kcore(dso))
+ name = machine->mmap_name;
+ else
+ name = dso__long_name(dso);
+
+ if (machine__is_host(machine)) {
+ misc = in_kernel ? PERF_RECORD_MISC_KERNEL
+ : PERF_RECORD_MISC_USER;
+ } else {
+ misc = in_kernel ? PERF_RECORD_MISC_GUEST_KERNEL
+ : PERF_RECORD_MISC_GUEST_USER;
+ }
+ perf_event__synthesize_build_id(tool, sample, machine,
+ inject_bid_offset_tool__process_build_id,
+ misc, bid, name);
+ }
+ }
+ dso__put(dso);
+out:
+ addr_location__exit(&al);
+}
+
+static bool read_u64(const u8 **inp, const u8 *in_end, u64 *val)
+{
+ if ((size_t)(in_end - *inp) < sizeof(u64))
+ return false;
+ memcpy(val, *inp, sizeof(u64));
+ *inp += sizeof(u64);
+ return true;
+}
+
+static bool copy_bytes(const u8 **inp, const u8 *in_end,
+ u8 **outp, const u8 *out_end, size_t len)
+{
+ if ((size_t)(in_end - *inp) < len || (size_t)(out_end - *outp) < len)
+ return false;
+ if (len)
+ memcpy(*outp, *inp, len);
+ *inp += len;
+ *outp += len;
+ return true;
+}
+
+static bool copy_sample_regs(const u8 **inp, const u8 *in_end,
+ u8 **outp, const u8 *out_end, u64 reg_mask)
+{
+ u64 abi;
+
+ if ((size_t)(in_end - *inp) < sizeof(u64))
+ return false;
+ memcpy(&abi, *inp, sizeof(u64));
+ if (!copy_bytes(inp, in_end, outp, out_end, sizeof(u64)))
+ return false;
+ if (abi == PERF_SAMPLE_REGS_ABI_NONE)
+ return true;
+
+ if (!copy_bytes(inp, in_end, outp, out_end,
+ hweight64(reg_mask) * sizeof(u64)))
+ return false;
+
+ if (abi & PERF_SAMPLE_REGS_ABI_SIMD) {
+ u64 hdr[4], vec_qwords, pred_qwords, simd_qwords;
+
+ if ((size_t)(in_end - *inp) < sizeof(hdr))
+ return false;
+ memcpy(hdr, *inp, sizeof(hdr));
+ if (!copy_bytes(inp, in_end, outp, out_end, sizeof(hdr)))
+ return false;
+ if (check_mul_overflow(hdr[0], hdr[1], &vec_qwords) ||
+ check_mul_overflow(hdr[2], hdr[3], &pred_qwords) ||
+ check_add_overflow(vec_qwords, pred_qwords, &simd_qwords) ||
+ simd_qwords > (size_t)(in_end - *inp) / sizeof(u64) ||
+ !copy_bytes(inp, in_end, outp, out_end,
+ (size_t)simd_qwords * sizeof(u64)))
+ return false;
+ }
+ return true;
+}
+
+static int inject_bid_offset_tool__sample(const struct perf_tool *tool,
+ union perf_event *event,
+ struct perf_sample *sample,
+ struct machine *machine)
+{
+ struct delegate_tool *dt =
+ container_of(tool, struct delegate_tool, tool);
+ struct inject_bid_offset_tool *ibo =
+ container_of(dt, struct inject_bid_offset_tool, tool);
+ const struct ip_callchain *raw_callchain = NULL;
+ struct evsel *evsel = sample->evsel;
+ __u64 sample_type = evsel->core.attr.sample_type;
+ const u8 *in, *in_end, *out_end;
+ struct perf_sample new_sample;
+ union perf_event *ev;
+ struct thread *thread;
+ u64 raw_ip = 0;
+ u8 *out;
+ int orig_sample_size, ret = -EFAULT;
+
+ if ((sample_type & (PERF_SAMPLE_IP | PERF_SAMPLE_CALLCHAIN)) == 0)
+ return ibo->tool.delegate->sample(ibo->tool.delegate, event,
+ sample, machine);
+
+ if (event->header.size < sizeof(event->header))
+ return -EFAULT;
+
+ if (symbol_conf.guest_code && !machine__is_host(machine))
+ thread = machine__findnew_guest_code(machine, sample->pid);
+ else
+ thread = machine__findnew_thread(machine, sample->pid,
+ sample->tid);
+
+ ev = (union perf_event *)ibo->event_copy;
+ ev->sample.header = event->header;
+
+ in = (const u8 *)event->sample.array;
+ in_end = (const u8 *)event + event->header.size;
+ out = (u8 *)ev->sample.array;
+ out_end = (const u8 *)ev + PERF_SAMPLE_MAX_SIZE - sizeof(u64);
+
+ if (sample_type & PERF_SAMPLE_IDENTIFIER) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_IP) {
+ if (!read_u64(&in, in_end, &raw_ip))
+ goto out_put;
+ if (thread)
+ mark_dso_hit(ibo, sample, machine, thread, raw_ip,
+ sample->cpumode);
+ }
+ if (sample_type & PERF_SAMPLE_TID) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_TIME) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_ADDR) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_ID) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_STREAM_ID) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_CPU) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_PERIOD) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_READ) {
+ u64 read_format = evsel->core.attr.read_format;
+ u64 first_word;
+
+ if ((size_t)(in_end - in) < sizeof(u64))
+ goto out_put;
+ memcpy(&first_word, in, sizeof(u64));
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
+ if (read_format & PERF_FORMAT_GROUP) {
+ size_t val_sz = sample_read_value_size(read_format);
+
+ if (first_word > (size_t)(in_end - in) / val_sz ||
+ !copy_bytes(&in, in_end, &out, out_end,
+ (size_t)first_word * val_sz))
+ goto out_put;
+ } else {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ if (read_format & PERF_FORMAT_LOST) {
+ if (!copy_bytes(&in, in_end, &out, out_end,
+ sizeof(u64)))
+ goto out_put;
+ }
+ }
+ }
+ if (sample_type & PERF_SAMPLE_CALLCHAIN) {
+ u64 callchain_nr;
+
+ if (!IS_ALIGNED((uintptr_t)in, sizeof(u64)) ||
+ (size_t)(in_end - in) < sizeof(u64))
+ goto out_put;
+ raw_callchain = (const struct ip_callchain *)in;
+ callchain_nr = raw_callchain->nr;
+ in += sizeof(u64);
+ if (callchain_nr > (size_t)(in_end - in) / sizeof(u64))
+ goto out_put;
+ if (thread) {
+ u8 cpumode = PERF_RECORD_MISC_USER;
+
+ for (u64 x = 0; x < callchain_nr; x++) {
+ u64 ip = raw_callchain->ips[x];
+
+ if (ip >= PERF_CONTEXT_MAX) {
+ switch (ip) {
+ case PERF_CONTEXT_HV:
+ cpumode = PERF_RECORD_MISC_HYPERVISOR;
+ break;
+ case PERF_CONTEXT_KERNEL:
+ cpumode = PERF_RECORD_MISC_KERNEL;
+ break;
+ case PERF_CONTEXT_USER:
+ cpumode = PERF_RECORD_MISC_USER;
+ break;
+ case PERF_CONTEXT_GUEST:
+ case PERF_CONTEXT_GUEST_KERNEL:
+ cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
+ break;
+ case PERF_CONTEXT_GUEST_USER:
+ cpumode = PERF_RECORD_MISC_GUEST_USER;
+ break;
+ case PERF_CONTEXT_USER_DEFERRED:
+ cpumode = PERF_RECORD_MISC_USER;
+ x++;
+ break;
+ default:
+ break;
+ }
+ continue;
+ }
+ mark_dso_hit(ibo, sample, machine, thread, ip,
+ cpumode);
+ }
+ }
+ in += (size_t)callchain_nr * sizeof(u64);
+ }
+ if (sample_type & PERF_SAMPLE_RAW) {
+ u32 raw_size;
+
+ if ((size_t)(in_end - in) < sizeof(u32))
+ goto out_put;
+ memcpy(&raw_size, in, sizeof(u32));
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u32)) ||
+ raw_size > (size_t)(in_end - in) ||
+ !copy_bytes(&in, in_end, &out, out_end, raw_size))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
+ u64 nr;
+
+ if ((size_t)(in_end - in) < sizeof(u64))
+ goto out_put;
+ memcpy(&nr, in, sizeof(u64));
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ if (evsel__has_branch_hw_idx(evsel)) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (nr > (size_t)(in_end - in) / sizeof(struct branch_entry) ||
+ !copy_bytes(&in, in_end, &out, out_end,
+ (size_t)nr * sizeof(struct branch_entry)))
+ goto out_put;
+ if (sample->branch_stack_cntr) {
+ if (nr > (size_t)(in_end - in) / sizeof(u64) ||
+ !copy_bytes(&in, in_end, &out, out_end,
+ (size_t)nr * sizeof(u64)))
+ goto out_put;
+ }
+ }
+ if (sample_type & PERF_SAMPLE_REGS_USER) {
+ if (!copy_sample_regs(&in, in_end, &out, out_end,
+ evsel->core.attr.sample_regs_user))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_STACK_USER) {
+ u64 sz;
+
+ if ((size_t)(in_end - in) < sizeof(u64))
+ goto out_put;
+ memcpy(&sz, in, sizeof(u64));
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ if (sz > 0) {
+ if (sz > (size_t)(in_end - in) ||
+ !copy_bytes(&in, in_end, &out, out_end, (size_t)sz) ||
+ !copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ }
+ if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_DATA_SRC) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_TRANSACTION) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_REGS_INTR) {
+ if (!copy_sample_regs(&in, in_end, &out, out_end,
+ evsel->core.attr.sample_regs_intr))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_PHYS_ADDR) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_CGROUP) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) {
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)))
+ goto out_put;
+ }
+ if (sample_type & PERF_SAMPLE_AUX) {
+ u64 sz;
+
+ if ((size_t)(in_end - in) < sizeof(u64))
+ goto out_put;
+ memcpy(&sz, in, sizeof(u64));
+ if (!copy_bytes(&in, in_end, &out, out_end, sizeof(u64)) ||
+ sz > (size_t)(in_end - in) ||
+ !copy_bytes(&in, in_end, &out, out_end, (size_t)sz))
+ goto out_put;
+ }
+
+ if (!IS_ALIGNED((uintptr_t)out, sizeof(u64)))
+ goto out_put;
+
+ if (sample_type & PERF_SAMPLE_IP) {
+ if ((size_t)(out_end - out) < 4 * sizeof(u64)) {
+ ret = -E2BIG;
+ goto out_put;
+ }
+ perf_event__inject_sample_buildid_array(thread, raw_ip,
+ sample->cpumode,
+ (__u64 *)out);
+ out += 4 * sizeof(u64);
+ }
+ if (sample_type & PERF_SAMPLE_CALLCHAIN) {
+ size_t cc_words = perf_event__inject_callchain_buildid_array(
+ thread, raw_callchain, (__u64 *)out,
+ (size_t)(out_end - out) / sizeof(u64), &ret);
+
+ if (!cc_words)
+ goto out_put;
+ out += cc_words * sizeof(u64);
+ }
+
+ ev->sample.header.size = (u16)(out - (u8 *)ev);
+
+ orig_sample_size = evsel->sample_size;
+ if (perf_event__rewrite_attr_for_build_id_offset(&evsel->core.attr)) {
+ ret = -EFAULT;
+ goto out_put;
+ }
+ evsel->sample_size = __evsel__sample_size(evsel->core.attr.sample_type);
+ perf_sample__init(&new_sample, /*all=*/true);
+ ret = __evsel__parse_sample(evsel, ev, &new_sample, /*needs_swap=*/false);
+ if (!ret) {
+ new_sample.evsel = evsel;
+ ret = ibo->tool.delegate->sample(ibo->tool.delegate, ev,
+ &new_sample, machine);
+ }
+ perf_sample__exit(&new_sample);
+ evsel->core.attr.sample_type = sample_type;
+ evsel->sample_size = orig_sample_size;
+out_put:
+ thread__put(thread);
+ return ret;
+}
+
+static int
+inject_bid_offset_tool__callchain_deferred(const struct perf_tool *tool,
+ union perf_event *event,
+ struct perf_sample *sample,
+ struct machine *machine)
+{
+ struct delegate_tool *dt =
+ container_of(tool, struct delegate_tool, tool);
+ struct inject_bid_offset_tool *ibo =
+ container_of(dt, struct inject_bid_offset_tool, tool);
+ size_t hdr_size = offsetof(struct perf_record_callchain_deferred,
+ nr_build_ids);
+ size_t max_size = PERF_SAMPLE_MAX_SIZE - sizeof(u64);
+ size_t orig_payload_size, id_size, new_size, max_words;
+ union perf_event *ev;
+ struct thread *thread;
+ u64 nr;
+ int ret = 0;
+
+ if ((event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) ||
+ !sample->callchain) {
+ return dt->delegate->callchain_deferred(dt->delegate, event,
+ sample, machine);
+ }
+
+ nr = sample->callchain->nr;
+ orig_payload_size = sizeof(event->callchain_deferred) + nr * sizeof(u64);
+ if (event->header.size < orig_payload_size)
+ return -EFAULT;
+ id_size = event->header.size - orig_payload_size;
+ if (hdr_size + id_size > max_size)
+ return -E2BIG;
+ max_words = (max_size - hdr_size - id_size) / sizeof(u64);
+
+ if (symbol_conf.guest_code && !machine__is_host(machine))
+ thread = machine__findnew_guest_code(machine, sample->pid);
+ else
+ thread = machine__findnew_thread(machine, sample->pid,
+ sample->tid);
+
+ for (u64 x = 0; x < nr; x++) {
+ u64 ip = sample->callchain->ips[x];
+
+ if (thread)
+ mark_dso_hit(ibo, sample, machine, thread, ip,
+ PERF_RECORD_MISC_USER);
+ }
+
+ ev = (union perf_event *)ibo->event_copy;
+ new_size = perf_event__inject_callchain_buildid_array(
+ thread, sample->callchain,
+ (__u64 *)&ev->callchain_deferred.nr_build_ids,
+ max_words, &ret);
+ if (!new_size) {
+ thread__put(thread);
+ return ret;
+ }
+ new_size = hdr_size + new_size * sizeof(__u64);
+ if (id_size) {
+ memcpy((void *)ev + new_size,
+ &event->callchain_deferred.ips[nr], id_size);
+ new_size += id_size;
+ }
+ ev->callchain_deferred.header = event->header;
+ ev->callchain_deferred.header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID;
+ ev->callchain_deferred.header.size = new_size;
+ ev->callchain_deferred.cookie = event->callchain_deferred.cookie;
+
+ ret = dt->delegate->callchain_deferred(dt->delegate, ev, sample, machine);
+ thread__put(thread);
+ return ret;
+}
+
+static int
+inject_bid_offset_tool__build_id(const struct perf_tool *tool,
+ struct perf_session *session,
+ union perf_event *event)
+{
+ struct delegate_tool *dt =
+ container_of(tool, struct delegate_tool, tool);
+
+ if (session)
+ perf_event__process_build_id(tool, session, event);
+ return dt->delegate->build_id(dt->delegate, session, event);
+}
+
+static int
+inject_bid_offset_tool__mmap(const struct perf_tool *tool,
+ union perf_event *event,
+ struct perf_sample *sample,
+ struct machine *machine)
+{
+ struct delegate_tool *dt =
+ container_of(tool, struct delegate_tool, tool);
+ u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
+
+ if (cpumode == PERF_RECORD_MISC_KERNEL ||
+ cpumode == PERF_RECORD_MISC_GUEST_KERNEL)
+ return dt->delegate->mmap(dt->delegate, event, sample, machine);
+
+ perf_event__process_mmap(tool, event, sample, machine);
+ return 0; // Drop user mmap events from output stream
+}
+static int
+inject_bid_offset_tool__mmap2(const struct perf_tool *tool,
+ union perf_event *event,
+ struct perf_sample *sample,
+ struct machine *machine)
+{
+ struct delegate_tool *dt =
+ container_of(tool, struct delegate_tool, tool);
+ u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
+
+ if (cpumode == PERF_RECORD_MISC_KERNEL ||
+ cpumode == PERF_RECORD_MISC_GUEST_KERNEL)
+ return dt->delegate->mmap2(dt->delegate, event, sample, machine);
+
+ perf_event__process_mmap2(tool, event, sample, machine);
+ return 0; // Drop user mmap2 events from output stream
+}
+
+struct perf_tool *inject_bid_offset_tool__new(struct perf_tool *delegate)
+{
+ struct inject_bid_offset_tool *ibo = zalloc(sizeof(*ibo));
+
+ if (!ibo)
+ return NULL;
+
+ hashmap__init(&ibo->synthesized_dsos, dso_ptr_hash, dso_ptr_equal, NULL);
+ delegate_tool__init(&ibo->tool, delegate);
+ ibo->tool.tool.sample = inject_bid_offset_tool__sample;
+ ibo->tool.tool.callchain_deferred = inject_bid_offset_tool__callchain_deferred;
+ ibo->tool.tool.build_id = inject_bid_offset_tool__build_id;
+ ibo->tool.tool.mmap = inject_bid_offset_tool__mmap;
+ ibo->tool.tool.mmap2 = inject_bid_offset_tool__mmap2;
+
+ return &ibo->tool.tool;
+}
+
+void inject_bid_offset_tool__delete(struct perf_tool *tool)
+{
+ struct inject_bid_offset_tool *ibo;
+ struct hashmap_entry *entry;
+ struct delegate_tool *dt;
+ size_t bkt;
+
+ if (!tool)
+ return;
+ dt = container_of(tool, struct delegate_tool, tool);
+ ibo = container_of(dt, struct inject_bid_offset_tool, tool);
+ hashmap__for_each_entry((&ibo->synthesized_dsos), entry, bkt) {
+ struct dso *dso = (struct dso *)entry->pvalue;
+
+ dso__put(dso);
+ }
+ hashmap__clear(&ibo->synthesized_dsos);
+ free(ibo);
+}
diff --git a/tools/perf/util/inject_bid_offset.h b/tools/perf/util/inject_bid_offset.h
new file mode 100644
index 000000000000..4ed2b3c0d85f
--- /dev/null
+++ b/tools/perf/util/inject_bid_offset.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_INJECT_BID_OFFSET_H
+#define __PERF_INJECT_BID_OFFSET_H
+
+#include <linux/perf_event.h>
+
+struct perf_tool;
+struct evlist;
+
+int perf_event__rewrite_attr_for_build_id_offset(struct perf_event_attr *attr);
+int perf_event__rewrite_attr_for_sample_ip(struct perf_event_attr *attr);
+struct perf_tool *inject_bid_offset_tool__new(struct perf_tool *delegate);
+void inject_bid_offset_tool__delete(struct perf_tool *tool);
+
+#endif /* __PERF_INJECT_BID_OFFSET_H */
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 17/19] perf record: Add --buildid-offset option
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (15 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 16/19] perf inject: Add --sample-buildids to convert samples to build ID + offset Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 18/19] perf test: Add BUILD_ID_OFFSET C unit tests Ian Rogers
2026-11-16 7:44 ` [PATCH v3 19/19] perf test: Add inject --sample-buildids and record --buildid-offset shell tests Ian Rogers
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Add the --buildid-offset flag to perf record to natively request
PERF_SAMPLE_BUILD_ID_OFFSET and PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET
from the kernel. This configures the evsel attributes by replacing
the standard IP and CALLCHAIN bits with the build ID offset equivalents.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/Documentation/perf-record.txt | 6 ++++++
tools/perf/builtin-record.c | 22 ++++++++++++++++++++--
tools/perf/util/evsel.c | 17 ++++++++++++++++-
tools/perf/util/record.c | 22 ++++++++++++++++++++++
tools/perf/util/record.h | 1 +
5 files changed, 65 insertions(+), 3 deletions(-)
diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 2351f7b4f5fc..d821303c923c 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -578,6 +578,12 @@ Specify vmlinux path which has debuginfo.
--buildid-all::
Record build-id of all DSOs regardless whether it's actually hit or not.
+--buildid-offset::
+Record build-id and DSO file offset instead of virtual instruction pointer
+addresses in samples and callchains (using PERF_SAMPLE_BUILD_ID_OFFSET and
+PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET). Implies --buildid-mmap and cannot be
+used with --no-buildid-mmap.
+
--buildid-mmap::
Legacy record build-id in map events option which is now the
default. Behaves indentically to --no-buildid. Disable with
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 085d3648a30a..5796f13ffe8c 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -936,7 +936,8 @@ static int record__config_tracking_events(struct record *rec)
* delay of waiting or event synthesis.
*/
if (opts->target.initial_delay || target__has_cpu(&opts->target) ||
- perf_pmus__num_core_pmus() > 1) {
+ perf_pmus__num_core_pmus() > 1 ||
+ (opts->buildid_offset && evlist__nr_entries(evlist) > 1)) {
/*
* User space tasks can migrate between CPUs, so when tracing
* selected CPUs, sideband for all CPUs is still needed.
@@ -3625,6 +3626,8 @@ static struct option __record_options[] = {
"file", "vmlinux pathname"),
OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
"Record build-id of all DSOs regardless of hits"),
+ OPT_BOOLEAN(0, "buildid-offset", &record.opts.buildid_offset,
+ "Record build id offset instead of IP"),
OPT_BOOLEAN_SET(0, "buildid-mmap", &record.buildid_mmap, &record.buildid_mmap_set,
"Record build-id in mmap events and skip build-id processing."),
OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
@@ -4162,6 +4165,20 @@ int cmd_record(int argc, const char **argv)
record.opts.record_switch_events = true;
}
+ if (rec->opts.buildid_offset) {
+ if (rec->buildid_mmap_set && !rec->buildid_mmap) {
+ pr_err("--buildid-offset cannot be used with --no-buildid-mmap.\n");
+ err = -EINVAL;
+ goto out_opts;
+ }
+ if (!perf_can_record_build_id()) {
+ pr_err("Missing support for build id in kernel mmap events required by --buildid-offset.\n");
+ err = -EINVAL;
+ goto out_opts;
+ }
+ rec->buildid_mmap = true;
+ }
+
if (rec->buildid_mmap && !perf_can_record_build_id()) {
pr_warning("Missing support for build id in kernel mmap events.\n"
"Disable this warning with --no-buildid-mmap\n");
@@ -4352,7 +4369,8 @@ int cmd_record(int argc, const char **argv)
evlist__warn_user_requested_cpus(rec->evlist, rec->opts.target.cpu_list);
- if (callchain_param.enabled && callchain_param.record_mode == CALLCHAIN_FP) {
+ if (callchain_param.enabled && callchain_param.record_mode == CALLCHAIN_FP &&
+ !rec->opts.buildid_offset) {
if (EM_HOST == EM_AARCH64)
add_leaf_frame_caller_opts_aarch64(&rec->opts);
}
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 49f6bbf08160..32c258764db9 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1745,7 +1745,7 @@ void evsel__config(struct evsel *evsel, const struct record_opts *opts,
attr->mmap2 = track && !perf_missing_features.mmap2;
attr->comm = track;
attr->build_id = track && opts->build_id;
- attr->defer_output = track && callchain && callchain->defer;
+ attr->defer_output = track && ((callchain && callchain->defer) || opts->buildid_offset);
/*
* ksymbol is tracked separately with text poke because it needs to be
@@ -1831,6 +1831,21 @@ void evsel__config(struct evsel *evsel, const struct record_opts *opts,
*/
evsel__apply_config_terms(evsel, opts, track);
+ if (opts->buildid_offset && !evsel__is_offcpu_event(evsel)) {
+ if ((evsel->core.attr.sample_type & PERF_SAMPLE_IP) || track) {
+ evsel__reset_sample_bit(evsel, IP);
+ evsel__set_sample_bit(evsel, BUILD_ID_OFFSET);
+ if (!evsel->no_aux_samples && !attr->exclude_user)
+ attr->defer_callchain = 1;
+ }
+ if ((evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN) &&
+ !attr->exclude_callchain_user) {
+ evsel__reset_sample_bit(evsel, CALLCHAIN);
+ evsel__set_sample_bit(evsel, CALLCHAIN_BUILD_ID_OFFSET);
+ attr->defer_callchain = 1;
+ }
+ }
+
evsel->ignore_missing_thread = opts->ignore_missing_thread;
/* The --period option takes the precedence. */
diff --git a/tools/perf/util/record.c b/tools/perf/util/record.c
index 38e8aee3106b..77337e1318ab 100644
--- a/tools/perf/util/record.c
+++ b/tools/perf/util/record.c
@@ -110,6 +110,28 @@ void evlist__config(struct evlist *evlist, struct record_opts *opts, struct call
evsel->core.attr.comm_exec = 1;
}
+ if (opts->buildid_offset) {
+ bool has_callchain_bid = false;
+
+ evlist__for_each_entry(evlist, evsel) {
+ if ((evsel->core.attr.sample_type &
+ PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) &&
+ !evsel->core.attr.exclude_callchain_user) {
+ has_callchain_bid = true;
+ break;
+ }
+ }
+ if (has_callchain_bid) {
+ evlist__for_each_entry(evlist, evsel) {
+ if (evsel->core.attr.defer_output &&
+ evsel__is_dummy_event(evsel)) {
+ evsel__set_sample_bit(evsel, CALLCHAIN_BUILD_ID_OFFSET);
+ evsel->core.attr.exclude_callchain_kernel = 1;
+ }
+ }
+ }
+ }
+
/* Configure leader sampling here now that the sample type is known */
evlist__for_each_entry(evlist, evsel)
evsel__config_leader_sampling(evsel, evlist);
diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h
index 411bb7276ad7..6c9568616177 100644
--- a/tools/perf/util/record.h
+++ b/tools/perf/util/record.h
@@ -55,6 +55,7 @@ struct record_opts {
bool kcore;
bool text_poke;
bool build_id;
+ bool buildid_offset;
unsigned int freq;
unsigned int mmap_pages;
unsigned int auxtrace_mmap_pages;
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 18/19] perf test: Add BUILD_ID_OFFSET C unit tests
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (16 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 17/19] perf record: Add --buildid-offset option Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
2026-11-16 7:44 ` [PATCH v3 19/19] perf test: Add inject --sample-buildids and record --buildid-offset shell tests Ian Rogers
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Add C unit test coverage for PERF_SAMPLE_BUILD_ID_OFFSET and
PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET:
- sample-parsing: round-trip synthesis and parsing
- mmap-thread-lookup: map lookup by build ID and DSO file offset
- bid-offset: sys_perf_event_open() attribute probing
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/Build | 1 +
tools/perf/tests/bid-offset.c | 61 +++++++++++++++++++++++++++
tools/perf/tests/builtin-test.c | 1 +
tools/perf/tests/mmap-thread-lookup.c | 49 ++++++++++++++++++++-
tools/perf/tests/sample-parsing.c | 60 +++++++++++++++++++++++++-
tools/perf/tests/tests.h | 1 +
6 files changed, 170 insertions(+), 3 deletions(-)
create mode 100644 tools/perf/tests/bid-offset.c
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 5eebb2ec5dde..d6fe1ba57a79 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -66,6 +66,7 @@ perf-test-y += expand-cgroup.o
perf-test-y += perf-time-to-tsc.o
perf-test-y += dlfilter-test.o
perf-test-y += sigtrap.o
+perf-test-y += bid-offset.o
perf-test-y += event_groups.o
perf-test-y += hybrid-merge.o
perf-test-y += symbols.o
diff --git a/tools/perf/tests/bid-offset.c b/tools/perf/tests/bid-offset.c
new file mode 100644
index 000000000000..0cf52caae047
--- /dev/null
+++ b/tools/perf/tests/bid-offset.c
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "tests.h"
+#include "debug.h"
+#include "evlist.h"
+#include "evsel.h"
+#include <linux/hw_breakpoint.h>
+#include <linux/perf_event.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <errno.h>
+#include "../perf-sys.h"
+
+static int test__bid_offset(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
+{
+ struct perf_event_attr attr = {
+ .type = PERF_TYPE_SOFTWARE,
+ .config = PERF_COUNT_SW_DUMMY,
+ .size = sizeof(attr),
+ .sample_type = PERF_SAMPLE_BUILD_ID_OFFSET,
+ .exclude_kernel = 1,
+ .exclude_hv = 1,
+ };
+ int fd;
+
+ fd = sys_perf_event_open(&attr, /*pid=*/0, /*cpu=*/-1, /*group_fd=*/-1, /*flags=*/0);
+ if (fd < 0) {
+ pr_debug("sys_perf_event_open failed with %d\n", errno);
+ if (errno == EINVAL || errno == EACCES || errno == EPERM) {
+ pr_debug("Kernel does not support or permit PERF_SAMPLE_BUILD_ID_OFFSET\n");
+ return TEST_SKIP;
+ }
+ return TEST_FAIL;
+ }
+ close(fd);
+
+ attr.sample_type = PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET;
+ attr.exclude_callchain_kernel = 1;
+ fd = sys_perf_event_open(&attr, /*pid=*/0, /*cpu=*/-1, /*group_fd=*/-1, /*flags=*/0);
+ if (fd < 0) {
+ pr_debug("sys_perf_event_open with callchain failed with %d\n", errno);
+ if (errno == EINVAL || errno == EACCES || errno == EPERM) {
+ pr_debug("Kernel does not support or permit PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET\n");
+ return TEST_SKIP;
+ }
+ return TEST_FAIL;
+ }
+ close(fd);
+
+ return TEST_OK;
+}
+
+static struct test_case bid_offset_tests[] = {
+ TEST_CASE_REASON("Test PERF_SAMPLE_BUILD_ID_OFFSET", bid_offset,
+ "not supported by kernel"),
+ { .name = NULL, }
+};
+
+struct test_suite suite__bid_offset = {
+ .desc = "Test PERF_SAMPLE_BUILD_ID_OFFSET",
+ .test_cases = bid_offset_tests,
+};
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 327483f96401..501d72fc7dcb 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -186,6 +186,7 @@ static struct test_suite *generic_tests[] = {
&suite__perf_time_to_tsc,
&suite__dlfilter,
&suite__sigtrap,
+ &suite__bid_offset,
&suite__event_groups,
&suite__hybrid_merge,
&suite__symbols,
diff --git a/tools/perf/tests/mmap-thread-lookup.c b/tools/perf/tests/mmap-thread-lookup.c
index dca8bbfe780a..7e312d5a1ba3 100644
--- a/tools/perf/tests/mmap-thread-lookup.c
+++ b/tools/perf/tests/mmap-thread-lookup.c
@@ -7,6 +7,7 @@
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include "debug.h"
#include "env.h"
#include "event.h"
@@ -17,7 +18,10 @@
#include "symbol.h"
#include "util/synthetic-events.h"
#include "thread.h"
+#include "dso.h"
+#include "build-id.h"
#include <internal/lib.h> // page_size
+#include <internal/rc_check.h>
#define THREADS 4
@@ -200,17 +204,58 @@ static int mmap_events(synth_cb synth)
.ip = (unsigned long) (td->map + 1),
}, &al);
- thread__put(thread);
-
if (!al.map) {
pr_debug("failed, couldn't find map\n");
err = -1;
+ thread__put(thread);
addr_location__exit(&al);
break;
}
pr_debug("map %p, addr %" PRIx64 "\n", al.map, map__start(al.map));
+ /* Verify lookup by build ID and offset */
+ {
+ struct build_id test_bid = {
+ .data = { [0] = i + 1 },
+ .size = BUILD_ID_SIZE,
+ };
+ struct dso *dso = dso__new("test_dso");
+ struct addr_location al2;
+ struct perf_build_id pbid = { .size = test_bid.size };
+ u64 offset = map__dso_map_ip(al.map,
+ (unsigned long)(td->map + 1));
+
+ if (!dso) {
+ pr_debug("failed to create test dso\n");
+ err = -1;
+ thread__put(thread);
+ addr_location__exit(&al);
+ break;
+ }
+ dso__set_build_id(dso, &test_bid);
+ dso__put(map__dso(al.map));
+ map__set_dso(al.map, dso);
+
+ memcpy(pbid.data, test_bid.data, test_bid.size);
+ addr_location__init(&al2);
+ thread__find_map(thread, &(struct perf_sample){
+ .cpumode = PERF_RECORD_MISC_USER,
+ .ip = 0,
+ .bid = {
+ .bid = &pbid,
+ .offset = offset,
+ },
+ }, &al2);
+
+ if (!RC_CHK_EQUAL(al2.map, al.map) || al2.addr != offset) {
+ pr_debug("failed, couldn't find map by build id and offset\n");
+ err = -1;
+ }
+ addr_location__exit(&al2);
+ }
+
+ thread__put(thread);
addr_location__exit(&al);
if (err)
break;
diff --git a/tools/perf/tests/sample-parsing.c b/tools/perf/tests/sample-parsing.c
index bd30f6d4c31b..41e8933bc2fc 100644
--- a/tools/perf/tests/sample-parsing.c
+++ b/tools/perf/tests/sample-parsing.c
@@ -218,6 +218,33 @@ static bool samples_same(struct perf_sample *s1,
}
}
+ if (type & PERF_SAMPLE_BUILD_ID_OFFSET) {
+ if (!s1->bid.bid || !s2->bid.bid ||
+ memcmp(s1->bid.bid, s2->bid.bid, sizeof(*s1->bid.bid))) {
+ pr_debug("Samples differ at 'bid.bid'\n");
+ return false;
+ }
+ COMP(bid.offset);
+ }
+
+ if (type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) {
+ COMP(callchain_bid.nr_build_ids);
+ COMP(callchain_bid.nr_offsets);
+ COMP(callchain_bid.build_ids_len);
+ if (!s1->callchain_bid.build_ids || !s2->callchain_bid.build_ids ||
+ memcmp(s1->callchain_bid.build_ids, s2->callchain_bid.build_ids,
+ s1->callchain_bid.build_ids_len)) {
+ pr_debug("Samples differ at 'callchain_bid.build_ids'\n");
+ return false;
+ }
+ if (!s1->callchain_bid.offsets || !s2->callchain_bid.offsets ||
+ memcmp(s1->callchain_bid.offsets, s2->callchain_bid.offsets,
+ s1->callchain_bid.nr_offsets * sizeof(u64))) {
+ pr_debug("Samples differ at 'callchain_bid.offsets'\n");
+ return false;
+ }
+ }
+
return true;
}
@@ -247,6 +274,26 @@ static int do_test(u64 sample_type, u64 sample_regs, u64 read_format)
const u32 raw_data[] = {0x12345678, 0x0a0b0c0d, 0x11020304, 0x05060708, 0 };
const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL};
const u64 aux_data[] = {0xa55a, 0, 0xeeddee, 0x0282028202820282};
+ const struct perf_build_id bid = {
+ .size = 20,
+ .data = {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ }
+ };
+ const u8 callchain_build_ids[48] = {
+ 20,
+ '9', '8', '7', '6', '5', '4', '3', '2', '1', '0',
+ '9', '8', '7', '6', '5', '4', '3', '2', '1', '0',
+ 20,
+ 'a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd',
+ 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f', 'a', 'b',
+ };
+ const u64 callchain_offsets[] = {
+ (1ULL << 56) | 201,
+ (2ULL << 56) | 202,
+ (1ULL << 56) | 203,
+ };
struct regs_dump user_regs = {
.abi = PERF_SAMPLE_REGS_ABI_64,
.mask = sample_regs,
@@ -295,6 +342,17 @@ static int do_test(u64 sample_type, u64 sample_regs, u64 read_format)
.size = sizeof(aux_data),
.data = (void *)aux_data,
},
+ .bid = {
+ .bid = (struct perf_build_id *)&bid,
+ .offset = 119,
+ },
+ .callchain_bid = {
+ .nr_build_ids = 2,
+ .nr_offsets = ARRAY_SIZE(callchain_offsets),
+ .build_ids_len = sizeof(callchain_build_ids),
+ .build_ids = (u8 *)callchain_build_ids,
+ .offsets = (u64 *)callchain_offsets,
+ },
};
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)];
@@ -547,7 +605,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_WEIGHT_STRUCT << 3 < PERF_SAMPLE_MAX) {
pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n");
return -1;
}
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index af822885890d..a65d209b649f 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -196,6 +196,7 @@ DECLARE_SUITE(expand_cgroup_events);
DECLARE_SUITE(perf_time_to_tsc);
DECLARE_SUITE(dlfilter);
DECLARE_SUITE(sigtrap);
+DECLARE_SUITE(bid_offset);
DECLARE_SUITE(event_groups);
DECLARE_SUITE(hybrid_merge);
DECLARE_SUITE(symbols);
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH v3 19/19] perf test: Add inject --sample-buildids and record --buildid-offset shell tests
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
` (17 preceding siblings ...)
2026-11-16 7:44 ` [PATCH v3 18/19] perf test: Add BUILD_ID_OFFSET C unit tests Ian Rogers
@ 2026-11-16 7:44 ` Ian Rogers
18 siblings, 0 replies; 20+ messages in thread
From: Ian Rogers @ 2026-11-16 7:44 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Ihor Solodrai, Leo Yan, Dmitry Vyukov, Steinar H . Gunderson,
Stephen Brennan, Thomas Falcon, linux-perf-users, bpf,
linux-kernel
Add shell tests verifying perf inject --sample-buildids (with basic
samples, callchains, pipe mode, and kernel samples) and perf record
--buildid-offset across per-thread, system-wide, workload, and callgraph
modes.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/shell/inject_bid_offset.sh | 179 ++++++++++++++++++++
tools/perf/tests/shell/record.sh | 74 ++++++++
2 files changed, 253 insertions(+)
create mode 100755 tools/perf/tests/shell/inject_bid_offset.sh
diff --git a/tools/perf/tests/shell/inject_bid_offset.sh b/tools/perf/tests/shell/inject_bid_offset.sh
new file mode 100755
index 000000000000..9be2e3323558
--- /dev/null
+++ b/tools/perf/tests/shell/inject_bid_offset.sh
@@ -0,0 +1,179 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# perf inject --sample-buildids test
+
+set -e
+set -o pipefail
+
+shelldir=$(dirname "$0")
+# shellcheck source=lib/perf_has_symbol.sh
+. "${shelldir}"/lib/perf_has_symbol.sh
+
+sym="noploop"
+
+skip_test_missing_symbol ${sym}
+
+temp_dir=$(mktemp -d /tmp/perf-test-bid-offset.XXXXXXXXXX)
+
+prog="perf test -w noploop"
+[ "$(uname -m)" = "s390x" ] && prog="$prog 3"
+err=0
+
+cleanup() {
+ local exit_code=${1:-$?}
+ trap - EXIT TERM INT
+ if [ "${exit_code}" -ne 0 ] || [ "${err}" -ne 0 ]; then
+ echo "Test failed! Preserving temp directory: ${temp_dir}"
+ exit 1
+ fi
+ if [[ "${temp_dir}" =~ ^/tmp/perf-test-bid-offset\. ]]; then
+ rm -rf "${temp_dir}"
+ fi
+ exit 0
+}
+
+trap_cleanup() {
+ local exit_code=$?
+ echo "Unexpected signal in ${FUNCNAME[1]}"
+ cleanup ${exit_code}
+}
+trap trap_cleanup EXIT TERM INT
+
+compare_script() {
+ local orig=$1
+ local bid=$2
+ local msg=$3
+
+ perf script -F -ip -i "${orig}" > "${temp_dir}/script_orig.txt"
+ perf script -F -ip -i "${bid}" > "${temp_dir}/script_bid.txt"
+
+ if [ ! -s "${temp_dir}/script_orig.txt" ]; then
+ echo "${msg} [Failed - empty perf script output]"
+ err=1
+ elif ! diff -q "${temp_dir}/script_orig.txt" "${temp_dir}/script_bid.txt"; then
+ echo "${msg} [Failed - perf script output differs]"
+ err=1
+ else
+ echo "${msg} [Success]"
+ fi
+}
+
+compare_report() {
+ local orig=$1
+ local bid=$2
+ local msg=$3
+
+ perf report -i "${orig}" --stdio | grep -v '^#' > "${temp_dir}/report_orig.txt"
+ perf report -i "${bid}" --stdio | grep -v '^#' > "${temp_dir}/report_bid.txt"
+
+ if ! diff -q "${temp_dir}/report_orig.txt" "${temp_dir}/report_bid.txt"; then
+ echo "${msg} [Failed - perf report output differs]"
+ err=1
+ else
+ echo "${msg} [Success]"
+ fi
+}
+
+test_basic() {
+ echo "Test basic build id offset injection"
+ local data="${temp_dir}/perf.data.basic"
+ local data2="${temp_dir}/perf.data2.basic"
+
+ perf record -e task-clock:u -o "${data}" ${prog} >/dev/null 2>&1
+ perf inject --sample-buildids -i "${data}" -o "${data2}" >/dev/null 2>&1
+
+ compare_script "${data}" "${data2}" "Basic injection"
+}
+
+test_callchain() {
+ echo "Test Callchain build id offset injection"
+ local data="${temp_dir}/perf.data.callchain"
+ local data2="${temp_dir}/perf.data2.callchain"
+
+ perf record --call-graph fp -e task-clock:u -o "${data}" ${prog} >/dev/null 2>&1
+ perf inject --sample-buildids -i "${data}" -o "${data2}" >/dev/null 2>&1
+
+ compare_script "${data}" "${data2}" "Callchain injection"
+}
+
+test_pipe() {
+ echo "Test pipe mode build id offset injection"
+ local data="${temp_dir}/perf.data.pipe"
+ local data2="${temp_dir}/perf.data2.pipe"
+
+ perf record -e task-clock:u -o - ${prog} 2>/dev/null | tee "${data}" | \
+ perf inject --sample-buildids -o "${data2}" >/dev/null 2>&1
+
+ compare_script "${data}" "${data2}" "Pipe mode injection"
+}
+
+test_report() {
+ echo "Test perf report consistency"
+ local data="${temp_dir}/perf.data.report"
+ local data2="${temp_dir}/perf.data2.report"
+
+ perf record -e task-clock:u -o "${data}" ${prog} >/dev/null 2>&1
+ perf inject --sample-buildids -i "${data}" -o "${data2}" >/dev/null 2>&1
+
+ compare_report "${data}" "${data2}" "Report consistency"
+}
+
+test_pipe_report() {
+ echo "Test pipe mode perf report consistency"
+ local data="${temp_dir}/perf.data.pipe_report"
+ local data2="${temp_dir}/perf.data2.pipe_report"
+
+ perf record -e task-clock:u -o - ${prog} 2>/dev/null | tee "${data}" | \
+ perf inject --sample-buildids -o "${data2}" >/dev/null 2>&1
+
+ compare_report "${data}" "${data2}" "Pipe Report consistency"
+}
+
+test_kernel() {
+ echo "Test kernel build id offset injection"
+ local data="${temp_dir}/perf.data.kernel"
+ local data2="${temp_dir}/perf.data2.kernel"
+ local kprog="dd if=/dev/urandom of=/dev/null bs=1M count=50"
+
+ # Not all systems allow kernel profiling, so we check if it succeeds first
+ if ! perf record -e task-clock:k -o "${data}" ${kprog} >/dev/null 2>&1; then
+ echo "Kernel injection [Skipped - permissions or support missing]"
+ return
+ fi
+ perf inject --sample-buildids -i "${data}" -o "${data2}" >/dev/null 2>&1
+
+ compare_script "${data}" "${data2}" "Kernel injection"
+}
+
+test_record_buildid_offset() {
+ echo "Test perf record --buildid-offset consistency"
+ local data="${temp_dir}/perf.data.record_orig"
+ local data2="${temp_dir}/perf.data2.record_bid"
+
+ perf record -e task-clock:u -o "${data}" ${prog} >/dev/null 2>&1
+ # We must have the binaries in .build-id cache, which the first record does natively.
+ # Now record with --buildid-offset.
+ if ! perf record --buildid-offset -e task-clock:u -o "${data2}" \
+ ${prog} >/dev/null 2>&1; then
+ echo "Record --buildid-offset consistency [Skipped - not supported by kernel]"
+ return
+ fi
+
+ # Verify symbolization resolves the workload symbol
+ if ! perf report -i "${data2}" --stdio | grep -q "${sym}"; then
+ echo "Record --buildid-offset consistency [Failed - missing ${sym} in perf report]"
+ err=1
+ else
+ echo "Record --buildid-offset consistency [Success]"
+ fi
+}
+
+test_basic
+test_callchain
+test_pipe
+test_report
+test_pipe_report
+test_kernel
+test_record_buildid_offset
+
+cleanup ${err}
diff --git a/tools/perf/tests/shell/record.sh b/tools/perf/tests/shell/record.sh
index e4789e2bcf43..80cee3eedcc3 100755
--- a/tools/perf/tests/shell/record.sh
+++ b/tools/perf/tests/shell/record.sh
@@ -740,6 +740,79 @@ test_ratio_to_prev() {
echo "Basic ratio-to-prev record test [Success]"
}
+check_buildid_offset() {
+ perf report --header -D -i "${perfdata}" 2>/dev/null | grep -q "BUILD_ID_OFFSET"
+}
+
+test_buildid_offset() {
+ echo "buildid-offset test"
+ local ret=0
+ perf_record_with_retry "${perfdata}" "check_buildid_offset" "perf test -w thloop" \
+ --buildid-offset || ret=$?
+ if [ $ret -eq 2 ]; then
+ echo "buildid-offset test [Skipped not supported]"
+ return
+ elif [ $ret -eq 1 ]; then
+ echo "buildid-offset test [Failed missing output]"
+ err=1
+ return
+ fi
+
+ # Expand test coverage
+ ret=0
+ perf_record_with_retry "${perfdata}" "check_per_thread" "perf test -w thloop" \
+ --buildid-offset --per-thread || ret=$?
+ if [ $ret -ne 0 ]; then
+ echo "buildid-offset test [Failed per-thread]"
+ err=1
+ return
+ fi
+
+ ret=0
+ perf_record_with_retry "${perfdata}" "check_system_wide" "perf test -w thloop" \
+ --buildid-offset -aB --synth=no || ret=$?
+ if [ $ret -eq 2 ]; then
+ echo "buildid-offset system-wide [Skipped not supported]"
+ elif [ $ret -ne 0 ]; then
+ echo "buildid-offset test [Failed system-wide]"
+ err=1
+ return
+ fi
+
+ ret=0
+ perf_record_with_retry "${perfdata}" "check_workload" "perf test -w thloop" \
+ --buildid-offset || ret=$?
+ if [ $ret -ne 0 ]; then
+ echo "buildid-offset test [Failed workload]"
+ err=1
+ return
+ fi
+
+ case $(uname -m)
+ in s390x)
+ cmd_flags="--call-graph dwarf -e cpu-clock";;
+ *)
+ cmd_flags="-g";;
+ esac
+
+ if ! perf record --buildid-offset -o "${perfdata}" $cmd_flags \
+ perf test -w brstack > /dev/null 2>&1
+ then
+ echo "buildid-offset test [Failed callgraph record]"
+ err=1
+ return
+ fi
+
+ if ! perf report -i "${perfdata}" 2>&1 | grep -q "${testsym2}"
+ then
+ echo "buildid-offset test [Failed callgraph missing symbol]"
+ err=1
+ return
+ fi
+
+ echo "buildid-offset test [Success]"
+}
+
# raise the limit of file descriptors to minimum
if [[ $default_fd_limit -lt $min_fd_limit ]]; then
ulimit -Sn $min_fd_limit
@@ -760,6 +833,7 @@ test_precise_max
test_callgraph
test_acr_sampling
test_ratio_to_prev
+test_buildid_offset
# restore the default value
ulimit -Sn $default_fd_limit
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 20+ messages in thread