Skip to content

Commit bfe4daf

Browse files
Stephane EranianPeter Zijlstra
authored andcommitted
perf/core: Add perf_clear_branch_entry_bitfields() helper
Make it simpler to reset all the info fields on the perf_branch_entry by adding a helper inline function. The goal is to centralize the initialization to avoid missing a field in case more are added. Signed-off-by: Stephane Eranian <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 3123109 commit bfe4daf

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

arch/x86/events/intel/lbr.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ void intel_pmu_lbr_disable_all(void)
769769
void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
770770
{
771771
unsigned long mask = x86_pmu.lbr_nr - 1;
772+
struct perf_branch_entry *br = cpuc->lbr_entries;
772773
u64 tos = intel_pmu_lbr_tos();
773774
int i;
774775

@@ -784,15 +785,11 @@ void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
784785

785786
rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
786787

787-
cpuc->lbr_entries[i].from = msr_lastbranch.from;
788-
cpuc->lbr_entries[i].to = msr_lastbranch.to;
789-
cpuc->lbr_entries[i].mispred = 0;
790-
cpuc->lbr_entries[i].predicted = 0;
791-
cpuc->lbr_entries[i].in_tx = 0;
792-
cpuc->lbr_entries[i].abort = 0;
793-
cpuc->lbr_entries[i].cycles = 0;
794-
cpuc->lbr_entries[i].type = 0;
795-
cpuc->lbr_entries[i].reserved = 0;
788+
perf_clear_branch_entry_bitfields(br);
789+
790+
br->from = msr_lastbranch.from;
791+
br->to = msr_lastbranch.to;
792+
br++;
796793
}
797794
cpuc->lbr_stack.nr = i;
798795
cpuc->lbr_stack.hw_idx = tos;
@@ -807,6 +804,7 @@ void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
807804
{
808805
bool need_info = false, call_stack = false;
809806
unsigned long mask = x86_pmu.lbr_nr - 1;
807+
struct perf_branch_entry *br = cpuc->lbr_entries;
810808
u64 tos = intel_pmu_lbr_tos();
811809
int i;
812810
int out = 0;
@@ -878,15 +876,14 @@ void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
878876
if (abort && x86_pmu.lbr_double_abort && out > 0)
879877
out--;
880878

881-
cpuc->lbr_entries[out].from = from;
882-
cpuc->lbr_entries[out].to = to;
883-
cpuc->lbr_entries[out].mispred = mis;
884-
cpuc->lbr_entries[out].predicted = pred;
885-
cpuc->lbr_entries[out].in_tx = in_tx;
886-
cpuc->lbr_entries[out].abort = abort;
887-
cpuc->lbr_entries[out].cycles = cycles;
888-
cpuc->lbr_entries[out].type = 0;
889-
cpuc->lbr_entries[out].reserved = 0;
879+
perf_clear_branch_entry_bitfields(br+out);
880+
br[out].from = from;
881+
br[out].to = to;
882+
br[out].mispred = mis;
883+
br[out].predicted = pred;
884+
br[out].in_tx = in_tx;
885+
br[out].abort = abort;
886+
br[out].cycles = cycles;
890887
out++;
891888
}
892889
cpuc->lbr_stack.nr = out;
@@ -951,6 +948,8 @@ static void intel_pmu_store_lbr(struct cpu_hw_events *cpuc,
951948
to = rdlbr_to(i, lbr);
952949
info = rdlbr_info(i, lbr);
953950

951+
perf_clear_branch_entry_bitfields(e);
952+
954953
e->from = from;
955954
e->to = to;
956955
e->mispred = get_lbr_mispred(info);
@@ -959,7 +958,6 @@ static void intel_pmu_store_lbr(struct cpu_hw_events *cpuc,
959958
e->abort = !!(info & LBR_INFO_ABORT);
960959
e->cycles = get_lbr_cycles(info);
961960
e->type = get_lbr_br_type(info);
962-
e->reserved = 0;
963961
}
964962

965963
cpuc->lbr_stack.nr = i;

include/linux/perf_event.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,22 @@ static inline void perf_sample_data_init(struct perf_sample_data *data,
10631063
data->txn = 0;
10641064
}
10651065

1066+
/*
1067+
* Clear all bitfields in the perf_branch_entry.
1068+
* The to and from fields are not cleared because they are
1069+
* systematically modified by caller.
1070+
*/
1071+
static inline void perf_clear_branch_entry_bitfields(struct perf_branch_entry *br)
1072+
{
1073+
br->mispred = 0;
1074+
br->predicted = 0;
1075+
br->in_tx = 0;
1076+
br->abort = 0;
1077+
br->cycles = 0;
1078+
br->type = 0;
1079+
br->reserved = 0;
1080+
}
1081+
10661082
extern void perf_output_sample(struct perf_output_handle *handle,
10671083
struct perf_event_header *header,
10681084
struct perf_sample_data *data,

0 commit comments

Comments
 (0)