perf/core: Check sample_type in sample data saving helper functions
Subsystems (7)
v5 cover perf/core: Check sample_type in sample data saving helper functions
Hello,
We use helper functions to save raw data, callchain and branch stack in perf_sample_data. These functions update perf_sample_data->dyn_size without checking event->attr.sample_type, which may result in unused space allocated in sample records. To prevent this from happening, this patchset enforces checking sample_type of an event in these helper functions.
Thanks,
Yabin
Changes since v1:
- Check event->attr.sample_type & PERF_SAMPLE_RAW before calling perf_sample_save_raw_data().
- Subject has been changed to reflect the change of solution.
Changes since v2:
- Move sample_type check into perf_sample_save_raw_data().
- (New patch) Move sample_type check into perf_sample_save_callchain().
- (New patch) Move sample_type check into perf_sample_save_brstack().
Changes since v3:
- Fix -Werror=implicit-function-declaration by moving has_branch_stack().
Changes since v4:
- Give a warning if data->sample_flags is already set when calling the helper functions.
Original commit message from v1:
perf/core: Trim dyn_size if raw data is absent
Original commit message from v2/v3:
perf/core: Save raw sample data conditionally based on sample type
Yabin Cui (3):
perf/core: Save raw sample data conditionally based on sample type perf/core: Check sample_type in perf_sample_save_callchain perf/core: Check sample_type in perf_sample_save_brstack
arch/s390/kernel/perf_cpum_cf.c | 2 +- arch/s390/kernel/perf_pai_crypto.c | 2 +- arch/s390/kernel/perf_pai_ext.c | 2 +- arch/x86/events/amd/core.c | 3 +-- arch/x86/events/amd/ibs.c | 5 ++--- arch/x86/events/core.c | 3 +-- arch/x86/events/intel/ds.c | 9 +++----- include/linux/perf_event.h | 26 +++++++++++++++++----- kernel/events/core.c | 35 +++++++++++++++--------------- kernel/trace/bpf_trace.c | 11 +++++----- 10 files changed, 55 insertions(+), 43 deletions(-)
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
PATCH v5 1/3 perf/core: Save raw sample data conditionally based on sample type
Currently, space for raw sample data is always allocated within sample records for both BPF output and tracepoint events. This leads to unused space in sample records when raw sample data is not requested.
This patch enforces checking sample type of an event in perf_sample_save_raw_data(). So raw sample data will only be saved if explicitly requested, reducing overhead when it is not needed.
--- arch/s390/kernel/perf_cpum_cf.c | 2 +- arch/s390/kernel/perf_pai_crypto.c | 2 +- arch/s390/kernel/perf_pai_ext.c | 2 +- arch/x86/events/amd/ibs.c | 2 +- include/linux/perf_event.h | 6 +++++ kernel/events/core.c | 35 +++++++++++++++--------------- kernel/trace/bpf_trace.c | 11 +++++----- 7 files changed, 34 insertions(+), 26 deletions(-)
arch/s390/kernel/perf_cpum_cf.c +1 -1
@@ -971,7 +971,7 @@ static int cfdiag_push_sample(struct perf_event *event,if (event->attr.sample_type &PERF_SAMPLE_RAW ) { raw.frag.size = cpuhw->usedss; raw.frag.data = cpuhw->stop; -perf_sample_save_raw_data (&data, &raw); +perf_sample_save_raw_data (&data, event, &raw); } overflow =perf_event_overflow (event, &data, ®s);
arch/s390/kernel/perf_pai_crypto.c +1 -1
@@ -444,7 +444,7 @@ static int paicrypt_push_sample(size_t rawsize, struct paicrypt_map *cpump,if (event->attr.sample_type &PERF_SAMPLE_RAW ) { raw.frag.size = rawsize; raw.frag.data = cpump->save; -perf_sample_save_raw_data (&data, &raw); +perf_sample_save_raw_data (&data, event, &raw); } overflow =perf_event_overflow (event, &data, ®s);
arch/s390/kernel/perf_pai_ext.c +1 -1
@@ -458,7 +458,7 @@ static int paiext_push_sample(size_t rawsize, struct paiext_map *cpump,if (event->attr.sample_type &PERF_SAMPLE_RAW ) { raw.frag.size = rawsize; raw.frag.data = cpump->save; -perf_sample_save_raw_data (&data, &raw); +perf_sample_save_raw_data (&data, event, &raw); } overflow =perf_event_overflow (event, &data, ®s);
arch/x86/events/amd/ibs.c +1 -1
@@ -1118,7 +1118,7 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs) .data = ibs_data.data, }, }; -perf_sample_save_raw_data (&data, &raw); +perf_sample_save_raw_data (&data, event, &raw); }if (perf_ibs == &perf_ibs_op)
include/linux/perf_event.h +6 -0
@@ -1243,12 +1243,18 @@ static inline void perf_sample_save_callchain(struct perf_sample_data *data, }static inline void perf_sample_save_raw_data (struct perf_sample_data *data, +struct perf_event *event,struct perf_raw_record *raw) {struct perf_raw_frag *frag = &raw->frag;u32 sum =0 ;int size; +if (!(event->attr.sample_type &PERF_SAMPLE_RAW )) +return ; +if (WARN_ON_ONCE (data->sample_flags &PERF_SAMPLE_RAW )) +return ; + do { sum += frag->size; if (perf_raw_frag_last(frag))
kernel/events/core.c +18 -17
@@ -10228,9 +10228,9 @@ static struct pmu perf_tracepoint = { };static int perf_tp_filter_match (struct perf_event *event, -struct perf_sample_data *data) +struct perf_raw_record *raw) { -void *record = data->raw->frag.data; +void *record = raw->frag.data;/* only top level events have filters set */ if (event->parent)
@@ -10242,7 +10242,7 @@ static int perf_tp_filter_match(struct perf_event *event, }static int perf_tp_event_match (struct perf_event *event, -struct perf_sample_data *data, +struct perf_raw_record *raw,struct pt_regs *regs) {if (event->hw.state &PERF_HES_STOPPED )
@@ -10253,7 +10253,7 @@ static int perf_tp_event_match(struct perf_event *event,if (event->attr.exclude_kernel && !user_mode (regs))return 0 ; -if (!perf_tp_filter_match (event, data)) +if (!perf_tp_filter_match (event, raw))return 0 ;return 1 ;
@@ -10279,6 +10279,7 @@ EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);static void __perf_tp_event_target_task (u64 count,void *record,struct pt_regs *regs,struct perf_sample_data *data, +struct perf_raw_record *raw,struct perf_event *event) {struct trace_entry *entry = record;
@@ -10288,13 +10289,17 @@ static void __perf_tp_event_target_task(u64 count, void *record,/* Cannot deliver synchronous signal to other task. */ if (event->attr.sigtrap)return ; -if (perf_tp_event_match (event, data, regs)) +if (perf_tp_event_match (event, raw, regs)) { +perf_sample_data_init (data,0 ,0 ); +perf_sample_save_raw_data (data, event, raw);perf_swevent_event (event, count, data, regs); + } }static void perf_tp_event_target_task (u64 count,void *record,struct pt_regs *regs,struct perf_sample_data *data, +struct perf_raw_record *raw,struct perf_event_context *ctx) {unsigned int cpu =smp_processor_id ();
@@ -10302,15 +10307,15 @@ static void perf_tp_event_target_task(u64 count, void *record,struct perf_event *event, *sibling; perf_event_groups_for_cpu_pmu(event, &ctx->pinned_groups, cpu, pmu) { -__perf_tp_event_target_task (count, record, regs, data, event); +__perf_tp_event_target_task (count, record, regs, data, raw, event);for_each_sibling_event (sibling, event) -__perf_tp_event_target_task (count, record, regs, data, sibling); +__perf_tp_event_target_task (count, record, regs, data, raw, sibling); } perf_event_groups_for_cpu_pmu(event, &ctx->flexible_groups, cpu, pmu) { -__perf_tp_event_target_task (count, record, regs, data, event); +__perf_tp_event_target_task (count, record, regs, data, raw, event);for_each_sibling_event (sibling, event) -__perf_tp_event_target_task (count, record, regs, data, sibling); +__perf_tp_event_target_task (count, record, regs, data, raw, sibling); } }
@@ -10328,15 +10333,10 @@ void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, }, }; -perf_sample_data_init (&data,0 ,0 ); -perf_sample_save_raw_data (&data, &raw); -perf_trace_buf_update (record, event_type);hlist_for_each_entry_rcu (event, head, hlist_entry) { -if (perf_tp_event_match (event, &data, regs)) { -perf_swevent_event (event, count, &data, regs); - +if (perf_tp_event_match (event, &raw, regs)) {/* * Here use the same on-stack perf_sample_data, * some members in data are event-specific and
@@ -10346,7 +10346,8 @@ void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,* because data->sample_flags is set. */ perf_sample_data_init (&data,0 ,0 ); -perf_sample_save_raw_data (&data, &raw); +perf_sample_save_raw_data (&data, event, &raw); +perf_swevent_event (event, count, &data, regs); } }
@@ -10363,7 +10364,7 @@ void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, goto unlock;raw_spin_lock (&ctx->lock); -perf_tp_event_target_task (count, record, regs, &data, ctx); +perf_tp_event_target_task (count, record, regs, &data, &raw, ctx);raw_spin_unlock (&ctx->lock); unlock:rcu_read_unlock ();
kernel/trace/bpf_trace.c +6 -5
@@ -620,7 +620,8 @@ static const struct bpf_func_proto bpf_perf_event_read_value_proto = { static __always_inline u64__bpf_perf_event_output (struct pt_regs *regs,struct bpf_map *map, -u64 flags,struct perf_sample_data *sd) +u64 flags,struct perf_raw_record *raw, +struct perf_sample_data *sd) { struct bpf_array *array = container_of(map, struct bpf_array, map);unsigned int cpu =smp_processor_id ();
@@ -645,6 +646,8 @@ __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,if (unlikely (event->oncpu != cpu))return -EOPNOTSUPP ; +perf_sample_save_raw_data (sd, event, raw); +return perf_event_output (event, sd, regs); }
@@ -688,9 +691,8 @@ BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, }perf_sample_data_init (sd,0 ,0 ); -perf_sample_save_raw_data (sd, &raw); - err =__bpf_perf_event_output (regs, map, flags, sd); + err =__bpf_perf_event_output (regs, map, flags, &raw, sd); out:this_cpu_dec (bpf_trace_nest_level);preempt_enable ();
@@ -749,9 +751,8 @@ u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,perf_fetch_caller_regs (regs);perf_sample_data_init (sd,0 ,0 ); -perf_sample_save_raw_data (sd, &raw); - ret =__bpf_perf_event_output (regs, map, flags, sd); + ret =__bpf_perf_event_output (regs, map, flags, &raw, sd); out:this_cpu_dec (bpf_event_output_nest_level);preempt_enable ();
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
[tip: perf/core] perf/core: Save raw sample data conditionally based on sample type
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: b9c44b91476b67327a521568a854babecc4070ab Gitweb: https://git.kernel.org/tip/b9c44b91476b67327a521568a854babecc4070ab Author: Yabin Cui <[email protected]> AuthorDate: Wed, 15 May 2024 12:36:07 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 19 Nov 2024 09:23:42 +01:00
perf/core: Save raw sample data conditionally based on sample type
Currently, space for raw sample data is always allocated within sample records for both BPF output and tracepoint events. This leads to unused space in sample records when raw sample data is not requested.
This patch enforces checking sample type of an event in perf_sample_save_raw_data(). So raw sample data will only be saved if explicitly requested, reducing overhead when it is not needed.
--- arch/s390/kernel/perf_cpum_cf.c | 2 +- arch/s390/kernel/perf_pai_crypto.c | 2 +- arch/s390/kernel/perf_pai_ext.c | 2 +- arch/x86/events/amd/ibs.c | 2 +- include/linux/perf_event.h | 6 +++++- kernel/events/core.c | 35 ++++++++++++++--------------- kernel/trace/bpf_trace.c | 11 ++++----- 7 files changed, 34 insertions(+), 26 deletions(-)
arch/s390/kernel/perf_cpum_cf.c +1 -1
@@ -981,7 +981,7 @@ static int cfdiag_push_sample(struct perf_event *event,if (event->attr.sample_type &PERF_SAMPLE_RAW ) { raw.frag.size = cpuhw->usedss; raw.frag.data = cpuhw->stop; -perf_sample_save_raw_data (&data, &raw); +perf_sample_save_raw_data (&data, event, &raw); } overflow =perf_event_overflow (event, &data, ®s);
arch/s390/kernel/perf_pai_crypto.c +1 -1
@@ -478,7 +478,7 @@ static int paicrypt_push_sample(size_t rawsize, struct paicrypt_map *cpump,if (event->attr.sample_type &PERF_SAMPLE_RAW ) { raw.frag.size = rawsize; raw.frag.data = cpump->save; -perf_sample_save_raw_data (&data, &raw); +perf_sample_save_raw_data (&data, event, &raw); } overflow =perf_event_overflow (event, &data, ®s);
arch/s390/kernel/perf_pai_ext.c +1 -1
@@ -503,7 +503,7 @@ static int paiext_push_sample(size_t rawsize, struct paiext_map *cpump,if (event->attr.sample_type &PERF_SAMPLE_RAW ) { raw.frag.size = rawsize; raw.frag.data = cpump->save; -perf_sample_save_raw_data (&data, &raw); +perf_sample_save_raw_data (&data, event, &raw); } overflow =perf_event_overflow (event, &data, ®s);
arch/x86/events/amd/ibs.c +1 -1
@@ -1118,7 +1118,7 @@ fail: .data = ibs_data.data, }, }; -perf_sample_save_raw_data (&data, &raw); +perf_sample_save_raw_data (&data, event, &raw); }if (perf_ibs == &perf_ibs_op)
include/linux/perf_event.h +6 -0
@@ -1287,12 +1287,18 @@ static inline void perf_sample_save_callchain(struct perf_sample_data *data, }static inline void perf_sample_save_raw_data (struct perf_sample_data *data, +struct perf_event *event,struct perf_raw_record *raw) {struct perf_raw_frag *frag = &raw->frag;u32 sum =0 ;int size; +if (!(event->attr.sample_type &PERF_SAMPLE_RAW )) +return ; +if (WARN_ON_ONCE (data->sample_flags &PERF_SAMPLE_RAW )) +return ; + do { sum += frag->size; if (perf_raw_frag_last(frag))
kernel/events/core.c +18 -17
@@ -10451,9 +10451,9 @@ static struct pmu perf_tracepoint = { };static int perf_tp_filter_match (struct perf_event *event, -struct perf_sample_data *data) +struct perf_raw_record *raw) { -void *record = data->raw->frag.data; +void *record = raw->frag.data;/* only top level events have filters set */ if (event->parent)
@@ -10465,7 +10465,7 @@ static int perf_tp_filter_match(struct perf_event *event, }static int perf_tp_event_match (struct perf_event *event, -struct perf_sample_data *data, +struct perf_raw_record *raw,struct pt_regs *regs) {if (event->hw.state &PERF_HES_STOPPED )
@@ -10476,7 +10476,7 @@ static int perf_tp_event_match(struct perf_event *event,if (event->attr.exclude_kernel && !user_mode (regs))return 0 ; -if (!perf_tp_filter_match (event, data)) +if (!perf_tp_filter_match (event, raw))return 0 ;return 1 ;
@@ -10502,6 +10502,7 @@ EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);static void __perf_tp_event_target_task (u64 count,void *record,struct pt_regs *regs,struct perf_sample_data *data, +struct perf_raw_record *raw,struct perf_event *event) {struct trace_entry *entry = record;
@@ -10511,13 +10512,17 @@ static void __perf_tp_event_target_task(u64 count, void *record,/* Cannot deliver synchronous signal to other task. */ if (event->attr.sigtrap)return ; -if (perf_tp_event_match (event, data, regs)) +if (perf_tp_event_match (event, raw, regs)) { +perf_sample_data_init (data,0 ,0 ); +perf_sample_save_raw_data (data, event, raw);perf_swevent_event (event, count, data, regs); + } }static void perf_tp_event_target_task (u64 count,void *record,struct pt_regs *regs,struct perf_sample_data *data, +struct perf_raw_record *raw,struct perf_event_context *ctx) {unsigned int cpu =smp_processor_id ();
@@ -10525,15 +10530,15 @@ static void perf_tp_event_target_task(u64 count, void *record,struct perf_event *event, *sibling; perf_event_groups_for_cpu_pmu(event, &ctx->pinned_groups, cpu, pmu) { -__perf_tp_event_target_task (count, record, regs, data, event); +__perf_tp_event_target_task (count, record, regs, data, raw, event);for_each_sibling_event (sibling, event) -__perf_tp_event_target_task (count, record, regs, data, sibling); +__perf_tp_event_target_task (count, record, regs, data, raw, sibling); } perf_event_groups_for_cpu_pmu(event, &ctx->flexible_groups, cpu, pmu) { -__perf_tp_event_target_task (count, record, regs, data, event); +__perf_tp_event_target_task (count, record, regs, data, raw, event);for_each_sibling_event (sibling, event) -__perf_tp_event_target_task (count, record, regs, data, sibling); +__perf_tp_event_target_task (count, record, regs, data, raw, sibling); } }
@@ -10551,15 +10556,10 @@ void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, }, }; -perf_sample_data_init (&data,0 ,0 ); -perf_sample_save_raw_data (&data, &raw); -perf_trace_buf_update (record, event_type);hlist_for_each_entry_rcu (event, head, hlist_entry) { -if (perf_tp_event_match (event, &data, regs)) { -perf_swevent_event (event, count, &data, regs); - +if (perf_tp_event_match (event, &raw, regs)) {/* * Here use the same on-stack perf_sample_data, * some members in data are event-specific and
@@ -10569,7 +10569,8 @@ void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,* because data->sample_flags is set. */ perf_sample_data_init (&data,0 ,0 ); -perf_sample_save_raw_data (&data, &raw); +perf_sample_save_raw_data (&data, event, &raw); +perf_swevent_event (event, count, &data, regs); } }
@@ -10586,7 +10587,7 @@ void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, goto unlock;raw_spin_lock (&ctx->lock); -perf_tp_event_target_task (count, record, regs, &data, ctx); +perf_tp_event_target_task (count, record, regs, &data, &raw, ctx);raw_spin_unlock (&ctx->lock); unlock:rcu_read_unlock ();
kernel/trace/bpf_trace.c +6 -5
@@ -619,7 +619,8 @@ static const struct bpf_func_proto bpf_perf_event_read_value_proto = { static __always_inline u64__bpf_perf_event_output (struct pt_regs *regs,struct bpf_map *map, -u64 flags,struct perf_sample_data *sd) +u64 flags,struct perf_raw_record *raw, +struct perf_sample_data *sd) { struct bpf_array *array = container_of(map, struct bpf_array, map);unsigned int cpu =smp_processor_id ();
@@ -644,6 +645,8 @@ __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,if (unlikely (event->oncpu != cpu))return -EOPNOTSUPP ; +perf_sample_save_raw_data (sd, event, raw); +return perf_event_output (event, sd, regs); }
@@ -687,9 +690,8 @@ BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, }perf_sample_data_init (sd,0 ,0 ); -perf_sample_save_raw_data (sd, &raw); - err =__bpf_perf_event_output (regs, map, flags, sd); + err =__bpf_perf_event_output (regs, map, flags, &raw, sd); out:this_cpu_dec (bpf_trace_nest_level);preempt_enable ();
@@ -748,9 +750,8 @@ u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,perf_fetch_caller_regs (regs);perf_sample_data_init (sd,0 ,0 ); -perf_sample_save_raw_data (sd, &raw); - ret =__bpf_perf_event_output (regs, map, flags, sd); + ret =__bpf_perf_event_output (regs, map, flags, &raw, sd); out:this_cpu_dec (bpf_event_output_nest_level);preempt_enable ();
PATCH v5 2/3 perf/core: Check sample_type in perf_sample_save_callchain
In reply to Yabin CuiCheck sample_type in perf_sample_save_callchain() to prevent saving callchain data when it isn't required.
--- arch/x86/events/amd/ibs.c | 3 +-- arch/x86/events/intel/ds.c | 6 ++---- include/linux/perf_event.h | 5 +++++ 3 files changed, 8 insertions(+), 6 deletions(-)
arch/x86/events/amd/ibs.c +1 -2
@@ -1129,8 +1129,7 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)* recorded as part of interrupt regs. Thus we need to use rip from * interrupt regs while unwinding call stack. */ -if (event->attr.sample_type &PERF_SAMPLE_CALLCHAIN ) -perf_sample_save_callchain (&data, event, iregs); +perf_sample_save_callchain (&data, event, iregs); throttle =perf_event_overflow (event, &data, ®s); out:
arch/x86/events/intel/ds.c +2 -4
@@ -1655,8 +1655,7 @@ static void setup_pebs_fixed_sample_data(struct perf_event *event,* previous PMI context or an (I)RET happened between the record and * PMI. */ -if (sample_type &PERF_SAMPLE_CALLCHAIN ) -perf_sample_save_callchain (data, event, iregs); +perf_sample_save_callchain (data, event, iregs);/* * We use the interrupt regs as a base because the PEBS record does not
@@ -1823,8 +1822,7 @@ static void setup_pebs_adaptive_sample_data(struct perf_event *event,* previous PMI context or an (I)RET happened between the record and * PMI. */ -if (sample_type &PERF_SAMPLE_CALLCHAIN ) -perf_sample_save_callchain (data, event, iregs); +perf_sample_save_callchain (data, event, iregs); *regs = *iregs;/* The ip in basic is EventingIP */
include/linux/perf_event.h +5 -0
@@ -1235,6 +1235,11 @@ static inline void perf_sample_save_callchain(struct perf_sample_data *data, {int size =1 ; +if (!(event->attr.sample_type &PERF_SAMPLE_CALLCHAIN )) +return ; +if (WARN_ON_ONCE (data->sample_flags &PERF_SAMPLE_CALLCHAIN )) +return ; + data->callchain =perf_callchain (event, regs); size += data->callchain->nr;
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
[tip: perf/core] perf/core: Check sample_type in perf_sample_save_callchain
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: f226805bc5f60adf03783d8e4cbfe303ccecd64e Gitweb: https://git.kernel.org/tip/f226805bc5f60adf03783d8e4cbfe303ccecd64e Author: Yabin Cui <[email protected]> AuthorDate: Wed, 15 May 2024 12:36:08 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 19 Nov 2024 09:23:42 +01:00
perf/core: Check sample_type in perf_sample_save_callchain
Check sample_type in perf_sample_save_callchain() to prevent saving callchain data when it isn't required.
--- arch/x86/events/amd/ibs.c | 3 +-- arch/x86/events/intel/ds.c | 6 ++---- include/linux/perf_event.h | 5 +++++ 3 files changed, 8 insertions(+), 6 deletions(-)
arch/x86/events/amd/ibs.c +1 -2
@@ -1129,8 +1129,7 @@ fail:* recorded as part of interrupt regs. Thus we need to use rip from * interrupt regs while unwinding call stack. */ -if (event->attr.sample_type &PERF_SAMPLE_CALLCHAIN ) -perf_sample_save_callchain (&data, event, iregs); +perf_sample_save_callchain (&data, event, iregs); throttle =perf_event_overflow (event, &data, ®s); out:
arch/x86/events/intel/ds.c +2 -4
@@ -1789,8 +1789,7 @@ static void setup_pebs_fixed_sample_data(struct perf_event *event,* previous PMI context or an (I)RET happened between the record and * PMI. */ -if (sample_type &PERF_SAMPLE_CALLCHAIN ) -perf_sample_save_callchain (data, event, iregs); +perf_sample_save_callchain (data, event, iregs);/* * We use the interrupt regs as a base because the PEBS record does not
@@ -1957,8 +1956,7 @@ static void setup_pebs_adaptive_sample_data(struct perf_event *event,* previous PMI context or an (I)RET happened between the record and * PMI. */ -if (sample_type &PERF_SAMPLE_CALLCHAIN ) -perf_sample_save_callchain (data, event, iregs); +perf_sample_save_callchain (data, event, iregs); *regs = *iregs;/* The ip in basic is EventingIP */
include/linux/perf_event.h +5 -0
@@ -1279,6 +1279,11 @@ static inline void perf_sample_save_callchain(struct perf_sample_data *data, {int size =1 ; +if (!(event->attr.sample_type &PERF_SAMPLE_CALLCHAIN )) +return ; +if (WARN_ON_ONCE (data->sample_flags &PERF_SAMPLE_CALLCHAIN )) +return ; + data->callchain =perf_callchain (event, regs); size += data->callchain->nr;
PATCH v5 3/3 perf/core: Check sample_type in perf_sample_save_brstack
In reply to Yabin CuiCheck sample_type in perf_sample_save_brstack() to prevent saving branch stack data when it isn't required.
--- arch/x86/events/amd/core.c | 3 +-- arch/x86/events/core.c | 3 +-- arch/x86/events/intel/ds.c | 3 +-- include/linux/perf_event.h | 15 ++++++++++----- 4 files changed, 13 insertions(+), 11 deletions(-)
arch/x86/events/amd/core.c +1 -2
@@ -998,8 +998,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 );
arch/x86/events/core.c +1 -2
@@ -1702,8 +1702,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 );
arch/x86/events/intel/ds.c +1 -2
@@ -1754,8 +1754,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,
include/linux/perf_event.h +10 -5
@@ -1276,6 +1276,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,
@@ -1283,6 +1288,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 );
@@ -1658,11 +1668,6 @@ extern void perf_bp_event(struct perf_event *event, void *data);# define perf_arch_bpf_user_pt_regs (regs) 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 ;
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
[tip: perf/core] perf/core: Check sample_type in perf_sample_save_brstack
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: faac6f105ef169e2e5678c14e1ffebf2a7d780b6 Gitweb: https://git.kernel.org/tip/faac6f105ef169e2e5678c14e1ffebf2a7d780b6 Author: Yabin Cui <[email protected]> AuthorDate: Wed, 15 May 2024 12:36:09 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 19 Nov 2024 09:23:42 +01:00
perf/core: Check sample_type in perf_sample_save_brstack
Check sample_type in perf_sample_save_brstack() to prevent saving branch stack data when it isn't required.
--- arch/x86/events/amd/core.c | 3 +-- arch/x86/events/core.c | 3 +-- arch/x86/events/intel/ds.c | 3 +-- include/linux/perf_event.h | 15 ++++++++++----- 4 files changed, 13 insertions(+), 11 deletions(-)
arch/x86/events/amd/core.c +1 -2
@@ -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 );
arch/x86/events/core.c +1 -2
@@ -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 );
arch/x86/events/intel/ds.c +1 -2
@@ -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,
include/linux/perf_event.h +10 -5
@@ -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 ;
Hello,
Quoted 33 lines · Yabin Cui
Hello,
We use helper functions to save raw data, callchain and branch stack in
perf_sample_data. These functions updateperf_sample_data->dyn_sizewithout checkingevent->attr.sample_type, which may result in unused space allocated in sample records. To prevent this from happening, this patchset enforces checkingsample_typeof an event in these helper functions.Thanks,
YabinChanges since v1:
- Checkevent->attr.sample_type&PERF_SAMPLE_RAWbefore callingperf_sample_save_raw_data().
- Subject has been changed to reflect the change of solution.Changes since v2:
- Movesample_typecheck intoperf_sample_save_raw_data().
- (New patch) Movesample_typecheck intoperf_sample_save_callchain().
- (New patch) Movesample_typecheck intoperf_sample_save_brstack().Changes since v3:
- Fix -Werror=implicit-function-declaration by movinghas_branch_stack().Changes since v4:
- Give a warning ifdata->sample_flagsis already set when calling the helper functions.Original commit message from v1:
perf/core: Trimdyn_sizeif raw data is absent
Quoted 8 lines
Original commit message from v2/v3:
perf/core: Save raw sample data conditionally based on sample typeYabin Cui (3):
perf/core: Save raw sample data conditionally based on sample type perf/core: Checksample_typeinperf_sample_save_callchainperf/core: Checksample_typeinperf_sample_save_brstack
Thanks,
Namhyung
Quoted 16 lines
arch/s390/kernel/perf_cpum_cf.c | 2 +- arch/s390/kernel/perf_pai_crypto.c | 2 +- arch/s390/kernel/perf_pai_ext.c | 2 +- arch/x86/events/amd/core.c | 3 +-- arch/x86/events/amd/ibs.c | 5 ++--- arch/x86/events/core.c | 3 +-- arch/x86/events/intel/ds.c | 9 +++----- include/linux/perf_event.h | 26 +++++++++++++++++----- kernel/events/core.c | 35 +++++++++++++++--------------- kernel/trace/bpf_trace.c | 11 +++++----- 10 files changed, 55 insertions(+), 43 deletions(-)--
2.45.0.rc1.225.g2a3ae87e7f-goog
Quoted 46 lines · Namhyung Kim
Hello,
Quoted 33 lines · Yabin Cui
Hello,
We use helper functions to save raw data, callchain and branch stack in
perf_sample_data. These functions updateperf_sample_data->dyn_sizewithout checkingevent->attr.sample_type, which may result in unused space allocated in sample records. To prevent this from happening, this patchset enforces checkingsample_typeof an event in these helper functions.Thanks,
YabinChanges since v1:
- Checkevent->attr.sample_type&PERF_SAMPLE_RAWbefore callingperf_sample_save_raw_data().
- Subject has been changed to reflect the change of solution.Changes since v2:
- Movesample_typecheck intoperf_sample_save_raw_data().
- (New patch) Movesample_typecheck intoperf_sample_save_callchain().
- (New patch) Movesample_typecheck intoperf_sample_save_brstack().Changes since v3:
- Fix -Werror=implicit-function-declaration by movinghas_branch_stack().Changes since v4:
- Give a warning ifdata->sample_flagsis already set when calling the helper functions.Original commit message from v1:
perf/core: Trimdyn_sizeif raw data is absentQuoted 8 lines
Original commit message from v2/v3:
perf/core: Save raw sample data conditionally based on sample typeYabin Cui (3):
perf/core: Save raw sample data conditionally based on sample type perf/core: Checksample_typeinperf_sample_save_callchainperf/core: Checksample_typeinperf_sample_save_brstack
Quoted 5 lines
Acked-by Namhyung Kim <[email protected]>Thanks,
Namhyung
Hi performance events subsystem maintainers,
The v5 patches were modified based on Peter's comments on the v4 patches. I'd be grateful if you could take a look when you have a moment.
Thank you for your time and consideration.
Thanks,
Yabin
Quoted 16 lines
Quoted 16 lines
arch/s390/kernel/perf_cpum_cf.c | 2 +- arch/s390/kernel/perf_pai_crypto.c | 2 +- arch/s390/kernel/perf_pai_ext.c | 2 +- arch/x86/events/amd/core.c | 3 +-- arch/x86/events/amd/ibs.c | 5 ++--- arch/x86/events/core.c | 3 +-- arch/x86/events/intel/ds.c | 9 +++----- include/linux/perf_event.h | 26 +++++++++++++++++----- kernel/events/core.c | 35 +++++++++++++++--------------- kernel/trace/bpf_trace.c | 11 +++++----- 10 files changed, 55 insertions(+), 43 deletions(-)--
2.45.0.rc1.225.g2a3ae87e7f-goog
Quoted 56 lines · Yabin Cui
Quoted 46 lines · Namhyung Kim
Hello,
Quoted 33 lines · Yabin Cui
Hello,
We use helper functions to save raw data, callchain and branch stack in
perf_sample_data. These functions updateperf_sample_data->dyn_sizewithout checkingevent->attr.sample_type, which may result in unused space allocated in sample records. To prevent this from happening, this patchset enforces checkingsample_typeof an event in these helper functions.Thanks,
YabinChanges since v1:
- Checkevent->attr.sample_type&PERF_SAMPLE_RAWbefore callingperf_sample_save_raw_data().
- Subject has been changed to reflect the change of solution.Changes since v2:
- Movesample_typecheck intoperf_sample_save_raw_data().
- (New patch) Movesample_typecheck intoperf_sample_save_callchain().
- (New patch) Movesample_typecheck intoperf_sample_save_brstack().Changes since v3:
- Fix -Werror=implicit-function-declaration by movinghas_branch_stack().Changes since v4:
- Give a warning ifdata->sample_flagsis already set when calling the helper functions.Original commit message from v1:
perf/core: Trimdyn_sizeif raw data is absentQuoted 8 lines
Original commit message from v2/v3:
perf/core: Save raw sample data conditionally based on sample typeYabin Cui (3):
perf/core: Save raw sample data conditionally based on sample type perf/core: Checksample_typeinperf_sample_save_callchainperf/core: Checksample_typeinperf_sample_save_brstackQuoted 5 lines
Acked-by Namhyung Kim <[email protected]>Thanks,
NamhyungHi performance events subsystem maintainers,
Quoted 8 lines
The v5 patches were modified based on Peter's comments on the v4 patches. I'd be grateful if you could take a look when you have a moment.
Thank you for your time and consideration.Thanks,
Yabin
Hi, friendly ping again for review?
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
arch/s390/kernel/perf_cpum_cf.c | 2 +- arch/s390/kernel/perf_pai_crypto.c | 2 +- arch/s390/kernel/perf_pai_ext.c | 2 +- arch/x86/events/amd/core.c | 3 +-- arch/x86/events/amd/ibs.c | 5 ++--- arch/x86/events/core.c | 3 +-- arch/x86/events/intel/ds.c | 9 +++----- include/linux/perf_event.h | 26 +++++++++++++++++----- kernel/events/core.c | 35 +++++++++++++++--------------- kernel/trace/bpf_trace.c | 11 +++++----- 10 files changed, 55 insertions(+), 43 deletions(-)--
2.45.0.rc1.225.g2a3ae87e7f-goog
Quoted 1 line · Yabin Cui
Hi, friendly ping again for review?
Sorry about the delay, this feel between the cracks. I've applied your series to tip:perf/core, nice work and thanks a lot!
Ingo