Skip to contentSkip to conversation

perf: Fix signed comparison in perf_adjust_period()

tip:perf/core tip-bot for Peter Zijlstra 1 participant 1 message Jun 8, 2010

lore
Share & tools
lore
#0 lore raw
Commit-ID:  f6ab91add6355e231e1c47897027b2a6ee4fa268
Gitweb:     http://git.kernel.org/tip/f6ab91add6355e231e1c47897027b2a6ee4fa268
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 4 Jun 2010 15:18:01 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 8 Jun 2010 18:43:00 +0200

perf: Fix signed comparison in perf_adjust_period()

Frederic reported that frequency driven swevents didn't work properly and even caused a division-by-zero error.

It turns out there are two bugs, the division-by-zero comes from a failure to deal with that in perf_calculate_period().

The other was more interesting and turned out to be a wrong comparison in perf_adjust_period(). The comparison was between an s64 and u64 and got implicitly converted to an unsigned comparison. The problem is that period_left is typically < 0, so it ended up being always true.

Cure this by making the local period variables s64.

LKML-Reference: <new-submission>

---
 kernel/perf_event.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
kernel/perf_event.c +4 -1
index 31d6afe..ff86c55 100644
@@ -1507,6 +1507,9 @@ do {					\
 		divisor = nsec * frequency;
 	}
 
+	if (!divisor)
+		return dividend;
+
 	return div64_u64(dividend, divisor);
 }
 
@@ -1529,7 +1532,7 @@ static int perf_event_start(struct perf_event *event)
 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
 {
 	struct hw_perf_event *hwc = &event->hw;
-	u64 period, sample_period;
+	s64 period, sample_period;
 	s64 delta;
 
 	period = perf_calculate_period(event, nsec, count);