]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf/core: Check sample_type in perf_sample_save_brstack
authorYabin Cui <yabinc@google.com>
Wed, 15 May 2024 19:36:09 +0000 (12:36 -0700)
committerIngo Molnar <mingo@kernel.org>
Tue, 19 Nov 2024 08:23:42 +0000 (09:23 +0100)
Check sample_type in perf_sample_save_brstack() to prevent
saving branch stack data when it isn't required.

Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Yabin Cui <yabinc@google.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240515193610.2350456-4-yabinc@google.com
arch/x86/events/amd/core.c
arch/x86/events/core.c
arch/x86/events/intel/ds.c
include/linux/perf_event.h

index b4a1a2576510e0ab5a67c18905631d3d06bf5110..30d6ceb4c8ad49fc0cf0bd83eacd2729712cf279 100644 (file)
@@ -1001,8 +1001,7 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs)
                if (!x86_perf_event_set_period(event))
                        continue;
 
-               if (has_branch_stack(event))
-                       perf_sample_save_brstack(&data, event, &cpuc->lbr_stack, NULL);
+               perf_sample_save_brstack(&data, event, &cpuc->lbr_stack, NULL);
 
                if (perf_event_overflow(event, &data, regs))
                        x86_pmu_stop(event, 0);
index c75c482d4c52f7eaaa3ba6529f96c2a758d6ebb3..8f218ac0d445c6f1a691291e8fd1e39507b9ebf4 100644 (file)
@@ -1707,8 +1707,7 @@ int x86_pmu_handle_irq(struct pt_regs *regs)
 
                perf_sample_data_init(&data, 0, event->hw.last_period);
 
-               if (has_branch_stack(event))
-                       perf_sample_save_brstack(&data, event, &cpuc->lbr_stack, NULL);
+               perf_sample_save_brstack(&data, event, &cpuc->lbr_stack, NULL);
 
                if (perf_event_overflow(event, &data, regs))
                        x86_pmu_stop(event, 0);
index 4990a24098072397d631cdd2de7c1f815a502153..acfd872075473864d6256db46427857e952566df 100644 (file)
@@ -1888,8 +1888,7 @@ static void setup_pebs_fixed_sample_data(struct perf_event *event,
        if (x86_pmu.intel_cap.pebs_format >= 3)
                setup_pebs_time(event, data, pebs->tsc);
 
-       if (has_branch_stack(event))
-               perf_sample_save_brstack(data, event, &cpuc->lbr_stack, NULL);
+       perf_sample_save_brstack(data, event, &cpuc->lbr_stack, NULL);
 }
 
 static void adaptive_pebs_save_regs(struct pt_regs *regs,
index 3ac202d971fb7669de006d9d7e05c7325e8b64f5..bf831b1485ff5b3a83e647feb56659904984591c 100644 (file)
@@ -1320,6 +1320,11 @@ static inline void perf_sample_save_raw_data(struct perf_sample_data *data,
        data->sample_flags |= PERF_SAMPLE_RAW;
 }
 
+static inline bool has_branch_stack(struct perf_event *event)
+{
+       return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
+}
+
 static inline void perf_sample_save_brstack(struct perf_sample_data *data,
                                            struct perf_event *event,
                                            struct perf_branch_stack *brs,
@@ -1327,6 +1332,11 @@ static inline void perf_sample_save_brstack(struct perf_sample_data *data,
 {
        int size = sizeof(u64); /* nr */
 
+       if (!has_branch_stack(event))
+               return;
+       if (WARN_ON_ONCE(data->sample_flags & PERF_SAMPLE_BRANCH_STACK))
+               return;
+
        if (branch_sample_hw_index(event))
                size += sizeof(u64);
        size += brs->nr * sizeof(struct perf_branch_entry);
@@ -1716,11 +1726,6 @@ static inline unsigned long perf_arch_guest_misc_flags(struct pt_regs *regs)
 # define perf_arch_guest_misc_flags(regs)      perf_arch_guest_misc_flags(regs)
 #endif
 
-static inline bool has_branch_stack(struct perf_event *event)
-{
-       return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
-}
-
 static inline bool needs_branch_stack(struct perf_event *event)
 {
        return event->attr.branch_sample_type != 0;