Skip to contentSkip to conversation

perf/core: Preserve the hardware index field for missing branch stacks

PATCH New Thaumy Cheng 2 participants 2 messages Nov 23 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf/core: Preserve the hardware index field for missing branch stacks

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.

---
 kernel/events/core.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/events/core.c +8 -1
index e870f12c3ad8..9c51c7a1ae80 100644
@@ -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

#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

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.

Quoted 8 lines

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1