Skip to content

Commit 75f8085

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf/x86/intel/pebs: Robustify PEBS buffer drain
Vince Weaver and Stephane Eranian reported warnings in the PEBS code when running the perf fuzzer. Stephane wrote: > I can reproduce the problem on my HSW running the fuzzer. > > I can see why this could be happening if you are mixing PEBS and non PEBS events > in the bottom 4 counters. I suspect: > for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) { > if ((counts[bit] == 0) && (error[bit] == 0)) > continue; > > This test is not correct when you have non-PEBS events mixed with > PEBS events and they overflow at the same time. They will have > counts[i] != 0 but error[i] == 0, and thus you fall thru the loop > and hit the assert. Or it is something along those lines. The only way I can make this work is if ->status only has !PEBS events set, because if it has both set we'll take that slow path which masks out the !PEBS bits. After masking there are 3 options: - there is one bit set, and its @bit, we increment counts[bit]. - there are multiple bits set, we increment error[] for each set bit, we do not increment counts[]. - there are no bits set, we do nothing. The intent was to never increment counts[] for !PEBS events. Now if we start out with only a single !PEBS event set, we'll pass the test and increment counts[] for a !PEBS and hit the warn. Reported-by: Vince Weaver <vincent.weaver@maine.edu> Reported-by: Stephane Eranian <eranian@google.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Arnaldo Carvalho de Melo <acme@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: kan.liang@intel.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 2a853e1 commit 75f8085

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

arch/x86/kernel/cpu/perf_event_intel_ds.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,7 @@ static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
11881188

11891189
for (at = base; at < top; at += x86_pmu.pebs_record_size) {
11901190
struct pebs_record_nhm *p = at;
1191+
u64 pebs_status;
11911192

11921193
/* PEBS v3 has accurate status bits */
11931194
if (x86_pmu.intel_cap.pebs_format >= 3) {
@@ -1198,12 +1199,17 @@ static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
11981199
continue;
11991200
}
12001201

1201-
bit = find_first_bit((unsigned long *)&p->status,
1202+
pebs_status = p->status & cpuc->pebs_enabled;
1203+
pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1;
1204+
1205+
bit = find_first_bit((unsigned long *)&pebs_status,
12021206
x86_pmu.max_pebs_events);
1203-
if (bit >= x86_pmu.max_pebs_events)
1204-
continue;
1205-
if (!test_bit(bit, cpuc->active_mask))
1207+
if (WARN(bit >= x86_pmu.max_pebs_events,
1208+
"PEBS record without PEBS event! status=%Lx pebs_enabled=%Lx active_mask=%Lx",
1209+
(unsigned long long)p->status, (unsigned long long)cpuc->pebs_enabled,
1210+
*(unsigned long long *)cpuc->active_mask))
12061211
continue;
1212+
12071213
/*
12081214
* The PEBS hardware does not deal well with the situation
12091215
* when events happen near to each other and multiple bits
@@ -1218,27 +1224,21 @@ static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
12181224
* one, and it's not possible to reconstruct all events
12191225
* that caused the PEBS record. It's called collision.
12201226
* If collision happened, the record will be dropped.
1221-
*
12221227
*/
1223-
if (p->status != (1 << bit)) {
1224-
u64 pebs_status;
1225-
1226-
/* slow path */
1227-
pebs_status = p->status & cpuc->pebs_enabled;
1228-
pebs_status &= (1ULL << MAX_PEBS_EVENTS) - 1;
1229-
if (pebs_status != (1 << bit)) {
1230-
for_each_set_bit(i, (unsigned long *)&pebs_status,
1231-
MAX_PEBS_EVENTS)
1232-
error[i]++;
1233-
continue;
1234-
}
1228+
if (p->status != (1ULL << bit)) {
1229+
for_each_set_bit(i, (unsigned long *)&pebs_status,
1230+
x86_pmu.max_pebs_events)
1231+
error[i]++;
1232+
continue;
12351233
}
1234+
12361235
counts[bit]++;
12371236
}
12381237

12391238
for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) {
12401239
if ((counts[bit] == 0) && (error[bit] == 0))
12411240
continue;
1241+
12421242
event = cpuc->events[bit];
12431243
WARN_ON_ONCE(!event);
12441244
WARN_ON_ONCE(!event->attr.precise_ip);

0 commit comments

Comments
 (0)