[PATCH] perf/core: Preserve the hardware index field for missing branch stacks
From: Thaumy Cheng
Date: Mon Nov 23 2026 - 00:07:21 EST
PERF_SAMPLE_BRANCH_HW_INDEX selects a branch-stack layout containing a
hardware index after nr, including when nr is zero. The generic fallback
for a missing branch stack currently emits only nr, unlike the non-NULL
stack path and the layout expected by perf's sample parser.
Emit an unknown hardware index (-1ULL) when the field is requested and
account for it when preparing the sample. Events without HW_INDEX and
samples with a supplied branch stack retain their existing layout.
This makes the fallback consistent with the ABI; no reproducer for reaching
this fallback with HW_INDEX enabled is claimed here.
Signed-off-by: Thaumy Cheng <thaumy.love@xxxxxxxxx>
---
kernel/events/core.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index e870f12c3ad8..9c51c7a1ae80 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -8499,11 +8499,16 @@ void perf_output_sample(struct perf_output_handle *handle,
perf_output_copy(handle, data->br_stack_cntr, size);
}
} else {
+ u64 nr = 0;
+ u64 hw_idx = -1ULL;
+
/*
* we always store at least the value of nr
*/
- u64 nr = 0;
perf_output_put(handle, nr);
+
+ if (branch_sample_hw_index(event))
+ perf_output_put(handle, hw_idx);
}
}
@@ -8837,6 +8842,8 @@ void perf_prepare_sample(struct perf_sample_data *data,
if (filtered_sample_type & PERF_SAMPLE_BRANCH_STACK) {
data->br_stack = NULL;
data->dyn_size += sizeof(u64);
+ if (branch_sample_hw_index(event))
+ data->dyn_size += sizeof(u64);
data->sample_flags |= PERF_SAMPLE_BRANCH_STACK;
}
--
2.56.0