Re: [PATCH v3 05/19] perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET sample types

From: Athira Rajeev

Date: Tue Nov 24 2026 - 23:34:36 EST




> On 25 Nov 2026, at 2:59 AM, Ian Rogers <irogers@xxxxxxxxxx> wrote:
>
> On Tue, Nov 24, 2026 at 9:33 AM Athira Rajeev <atrajeev@xxxxxxxxxxxxx> wrote:
>>
>>> On 16 Nov 2026, at 1:14 PM, Ian Rogers <irogers@xxxxxxxxxx> wrote:
>>>
>>> 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@xxxxxxxxxx>
>>> Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
>>
>> Hi Ian
>>
>> I have few queries and adding them below
>
> [snip]
>
>>> 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;
>>
>>
>> in alloc_callchain_buffers(), we are changing size unconditionally to :
>>
>> size = perf_callchain_context__sizeof() * PERF_NR_CONTEXTS;
>>
>> Even if PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET is not set,
>> still the size grows for every case.
>>
>> This grows each per-CPU, per-context slot by the build ID table and the
>> bpf_stack_build_id scratch area, and it is not conditional on the new
>> sample types. get_callchain_buffers() is called for plain
>> PERF_SAMPLE_CALLCHAIN too, so as I read it, a plain "perf record -g"
>> (or any BPF tool using callchains) would also pay the extra memory on
>> every possible CPU, without --buildid-offset.
>>
>> Is that the intent? If not, could the extra scratch space be allocated
>> only when an event with PERF_SAMPLE_BUILD_ID_OFFSET or
>> PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET exists? It would also be good to
>> state the per-CPU memory cost in the commit message.
>
> Agreed, plain PERF_SAMPLE_CALLCHAIN shouldn't pay that extra per-CPU
> memory cost. Note that single-IP PERF_SAMPLE_BUILD_ID_OFFSET resolves
> on the stack when not combined with
> PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET, so only
> PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET needs the per-CPU scratch
> buffers.
>
> In v4 I've split the build-ID callchain scratch buffers into a
> separate refcounted allocation (get_callchain_build_id_buffers() /
> put_callchain_build_id_buffers(), tracked via
> PERF_ATTACH_CALLCHAIN_BUILD_ID) that is only allocated while at least
> one PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET event is active. Plain
> PERF_SAMPLE_CALLCHAIN events now incur zero extra memory overhead.
> I've also documented the exact per-CPU memory cost in the commit
> message (10,560 bytes per recursion slot, or 42,240 bytes / 41.25 KiB
> per possible CPU across PERF_NR_CONTEXTS=4 at the default
> perf_event_max_stack=127).

Hi Ian

Sure, Thanks for addressing the suggestion in V4
I will check the latest changes in V4

>
>>>
>>> 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;
>>
>> perf_bpf_to_sample_bid_offset() and perf_callchain_encode_build_ids() only ever
>> write size = BUILD_ID_SIZE_MAX for a resolved build ID, and size = 0 for the unresolved case.
>> As far as I can see the table in PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET,
>> therefore always contains BUILD_ID_SIZE_MAX-byte entries, although patch 03 documents it
>> as variable-length {u8 size; u8 data[size]}.
>>
>> Is there a case where size can be anything other than 0 or 20? If not,
>> could the UAPI comment in patch 03 say so?
>
> ELF build IDs can have lengths other than 20 bytes (e.g. 8-byte xxhash
> / --build-id=0x... or 16-byte MD5/UUID, alongside 20-byte SHA-1),
> matching struct perf_build_id::size (1..20) in PERF_RECORD_MMAP2.
Ok.
>
> In the kernel, stack_map_get_build_id_offset() currently uses struct
> bpf_stack_build_id which doesn't carry the parsed note length and
> zero-pads shorter build IDs to BUILD_ID_SIZE_MAX (20), so
> kernel-emitted table entries currently have size == 20. However,
> user-space tools such as "perf inject --sample-buildids" write the
> exact DSO build ID length (1 <= size <= 20), and keeping size in the
> UAPI format also allows the kernel to emit unpadded lengths in the
> future without an ABI break. Note that table entries in
> PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET never have size == 0 (unresolved
> frames are encoded via index 0x00 in offsets[] without adding a table
> entry).

Got it.
>
> I've updated the UAPI comment in Patch 03 for v4 to explain this
> explicitly (1 <= size <= 20, with the kernel currently zero-padding to
> 20 bytes via bpf_stack_build_id while user-space tools may emit the
> exact unpadded length).
Sure that helps

Thanks
Athira
>
> Thanks!
> Ian
>
>> Thanks
>> Athira