Skip to contentSkip to conversation

perf/core: Fix broken throttling bugs

PATCH Applied Qing Wong 3 participants 10 messages Apr 5, 2025 – Apr 30, 2025 perf events

lore
Share & tools
lore

Qing Wong

author
#0 lore raw

cover perf/core: Fix broken throttling bugs

From: Qing Wang <[email protected]>

There is a bug that broken throttling, this patchset intends to improve the accuracy of throttling.

This patchset does two changes:
- Revert a specific patch to avoid broken throttling when

    max_samples_per_tick>1.

- Add throttling judgement when max_samples_per_tick=1.

base-commit: acc4d5ff0b61

Qing Wang (2):
Revert "perf/core: Fix hardlockup failure caused by perf throttle" perf/core: Fix broken throttling when max_samples_per_tick=1

 kernel/events/core.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 Revert "perf/core: Fix hardlockup failure caused by perf throttle"

From: Qing Wang <[email protected]>

This reverts commit 15def34e2635ab7e0e96f1bc32e1b69609f14942.

The hardlockup failure does not exist because:
1. The hardlockup's watchdog event is a pinned event, which exclusively occupies a dedicated PMC (Performance Monitoring Counter) and is unaffected by PMC scheduling.
2. The hardware event throttling mechanism only disables the specific PMC where throttling occurs, without impacting other PMCs. Consequently, The hardlockup event's dedicated PMC remains entirely unaffected.

---
 kernel/events/core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/events/core.c +2 -2
index 0bb21659e252..29cdb240e104 100644
@@ -10049,8 +10049,8 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle)
 		hwc->interrupts = 1;
 	} else {
 		hwc->interrupts++;
-		if (unlikely(throttle &&
-			     hwc->interrupts > max_samples_per_tick)) {
+		if (unlikely(throttle
+			     && hwc->interrupts >= max_samples_per_tick)) {
 			__this_cpu_inc(perf_throttled_count);
 			tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
 			hwc->interrupts = MAX_INTERRUPTS;
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH 1/2] Revert "perf/core: Fix hardlockup failure caused by perf throttle"

Quoted 22 lines · Qing Wong

From: Qing Wang <[email protected]>

This reverts commit 15def34e2635ab7e0e96f1bc32e1b69609f14942.

The hardlockup failure does not exist because:
1. The hardlockup's watchdog event is a pinned event, which exclusively occupies a dedicated PMC (Performance Monitoring Counter) and is unaffected by PMC scheduling.
2. The hardware event throttling mechanism only disables the specific PMC where throttling occurs, without impacting other PMCs. Consequently, The hardlockup event's dedicated PMC remains entirely unaffected.

---
 kernel/events/core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/events/core.c +0 -0
index 0bb21659e252..29cdb240e104 100644

kernel/events/core.c __perf_event_account_interrupt(struct perf_event *event, int throttle) · line 10053

Quoted 7 lines
kernel/events/core.c +2 -2
@@ -10049,8 +10049,8 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle)
 		hwc->interrupts = 1;
 	} else {
 		hwc->interrupts++;
-		if (unlikely(throttle &&
-			     hwc->interrupts > max_samples_per_tick)) {
+		if (unlikely(throttle
+			     && hwc->interrupts >= max_samples_per_tick)) {

Well, it restores bad coding style. The referred commit also states that max_samples_per_tick can be 1, at which point we'll always throttle the thing, since we've just increased.

That is, the part of the old commit that argued about e050e3f0a71bf flipped the compare and the increment is still true. So even though it might not be related to the hardlockup problem, I still don't think the patch was wrong.

author
#3 lore raw

PATCH 2/2 perf/core: Fix broken throttling when max_samples_per_tick=1

In reply to Qing Wong

From: Qing Wang <[email protected]>

According to the throttling mechanism, the pmu interrupts number can not exceed the max_samples_per_tick in one tick. But this mechanism is ineffective when max_samples_per_tick=1, because the throttling check is skipped during the first interrupt and only performed when the second interrupt arrives.

Perhaps this bug may cause little influence in one tick, but if in a larger time scale, the problem can not be underestimated.

When max_samples_per_tick = 1:

Allowed-interrupts-per-second max-samples-per-second  default-HZ  ARCH
200                           100                     100         X86
500                           250                     250         ARM64

...
Obviously, the pmu interrupt number far exceed the user's expect.

Fixes e050e3f0a71b ("perf: Fix broken interrupt rate throttling")
---
 kernel/events/core.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
kernel/events/core.c +8 -9
index 29cdb240e104..4ac2ac988ddc 100644
@@ -10047,16 +10047,15 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle)
 	if (seq != hwc->interrupts_seq) {
 		hwc->interrupts_seq = seq;
 		hwc->interrupts = 1;
-	} else {
+	} else
 		hwc->interrupts++;
-		if (unlikely(throttle
-			     && hwc->interrupts >= max_samples_per_tick)) {
-			__this_cpu_inc(perf_throttled_count);
-			tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
-			hwc->interrupts = MAX_INTERRUPTS;
-			perf_log_throttle(event, 0);
-			ret = 1;
-		}
+
+	if (unlikely(throttle && hwc->interrupts >= max_samples_per_tick)) {
+		__this_cpu_inc(perf_throttled_count);
+		tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
+		hwc->interrupts = MAX_INTERRUPTS;
+		perf_log_throttle(event, 0);
+		ret = 1;
 	}
 
 	if (event->attr.freq) {
Signature · 1 line
2.43.0
maintainer
#4 lore raw

Re: [PATCH 2/2] perf/core: Fix broken throttling when max_samples_per_tick=1

Quoted 41 lines · Qing Wong

From: Qing Wang <[email protected]>

According to the throttling mechanism, the pmu interrupts number can not exceed the max_samples_per_tick in one tick. But this mechanism is ineffective when max_samples_per_tick=1, because the throttling check is skipped during the first interrupt and only performed when the second interrupt arrives.

Perhaps this bug may cause little influence in one tick, but if in a larger time scale, the problem can not be underestimated.

When max_samples_per_tick = 1:

Allowed-interrupts-per-second max-samples-per-second  default-HZ  ARCH
200                           100                     100         X86
500                           250                     250         ARM64

...
Obviously, the pmu interrupt number far exceed the user's expect.

Fixes e050e3f0a71b ("perf: Fix broken interrupt rate throttling")
---
 kernel/events/core.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
kernel/events/core.c +1 -7
index 29cdb240e104..4ac2ac988ddc 100644
@@ -10047,16 +10047,15 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle)
 	if (seq != hwc->interrupts_seq) {
 		hwc->interrupts_seq = seq;
 		hwc->interrupts = 1;
-	} else {
+	} else
 		hwc->interrupts++;
-		if (unlikely(throttle
-			     && hwc->interrupts >= max_samples_per_tick)) {
-			__this_cpu_inc(perf_throttled_count);
-			tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
-			hwc->interrupts = MAX_INTERRUPTS;
-			perf_log_throttle(event, 0);

kernel/events/core.c __perf_event_account_interrupt(struct perf_event *event, int throttle) · line 10059

Quoted 10 lines
kernel/events/core.c +7 -2
-			ret = 1;
-		}
+
+	if (unlikely(throttle && hwc->interrupts >= max_samples_per_tick)) {
+		__this_cpu_inc(perf_throttled_count);
+		tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
+		hwc->interrupts = MAX_INTERRUPTS;
+		perf_log_throttle(event, 0);
+		ret = 1;
 	}

Fair enough I suppose. I'll make this apply without that revert -- it seems pointless to have that in between.

author
#5 lore raw

Re: [PATCH 2/2] perf/core: Fix broken throttling when max_samples_per_tick=1

Thank you very much for your review. Do you need me to reorganize the patch and send it out? Because if only the second patch is accepted, its context won't match the current mainline code.

Quoted 2 lines · Peter Zijlstra

Fair enough I suppose. I'll make this apply without that revert -- it seems pointless to have that in between.

maintainer
#6 lore raw

Re: [PATCH 2/2] perf/core: Fix broken throttling when max_samples_per_tick=1

Quoted 3 lines · Qing Wang

Thank you very much for your review. Do you need me to reorganize the patch and send it out? Because if only the second patch is accepted, its context won't match the current mainline code.

I've stomped on it a bit and pushed out to queue/perf/core.

If all looks well, and the robots don't have a fit because I failed to compile test the thing, it should eventually make its way into tip.

author
#7 lore raw

Re: [PATCH 2/2] perf/core: Fix broken throttling when max_samples_per_tick=1

Thank you again. This has kick-started my journey of contributing to the Linux Kernel community, and it's really awesome.

Quoted 4 lines · Peter Zijlstra

I've stomped on it a bit and pushed out to queue/perf/core.

If all looks well, and the robots don't have a fit because I failed to compile test the thing, it should eventually make its way into tip.

merge bot Applied
#8 lore raw

[tip: perf/core] perf/core: Fix broken throttling when max_samples_per_tick=1

In reply to Qing Wong
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:     f51972e6f8b9a737b2b3eb588069acb538fa72de
Gitweb:        https://git.kernel.org/tip/f51972e6f8b9a737b2b3eb588069acb538fa72de
Author:        Qing Wang <[email protected]>
AuthorDate:    Sat, 05 Apr 2025 22:16:35 +08:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 25 Apr 2025 14:55:22 +02:00

perf/core: Fix broken throttling when max_samples_per_tick=1

According to the throttling mechanism, the pmu interrupts number can not exceed the max_samples_per_tick in one tick. But this mechanism is ineffective when max_samples_per_tick=1, because the throttling check is skipped during the first interrupt and only performed when the second interrupt arrives.

Perhaps this bug may cause little influence in one tick, but if in a larger time scale, the problem can not be underestimated.

When max_samples_per_tick = 1:

Allowed-interrupts-per-second max-samples-per-second  default-HZ  ARCH
200                           100                     100         X86
500                           250                     250         ARM64

...
Obviously, the pmu interrupt number far exceed the user's expect.

Fixes e050e3f0a71b ("perf: Fix broken interrupt rate throttling")
---
 kernel/events/core.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
kernel/events/core.c +8 -8
index 3c69a1a..05136e8 100644
@@ -10065,14 +10065,14 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle)
 		hwc->interrupts = 1;
 	} else {
 		hwc->interrupts++;
-		if (unlikely(throttle &&
-			     hwc->interrupts > max_samples_per_tick)) {
-			__this_cpu_inc(perf_throttled_count);
-			tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
-			hwc->interrupts = MAX_INTERRUPTS;
-			perf_log_throttle(event, 0);
-			ret = 1;
-		}
+	}
+
+	if (unlikely(throttle && hwc->interrupts >= max_samples_per_tick)) {
+		__this_cpu_inc(perf_throttled_count);
+		tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
+		hwc->interrupts = MAX_INTERRUPTS;
+		perf_log_throttle(event, 0);
+		ret = 1;
 	}
 
 	if (event->attr.freq) {
author
#9 lore raw
In reply to Qing Wong

Hello,

PING.

Thanks,
Qing.

Quoted 19 lines · Qing Wong

From: Qing Wang <[email protected]>

There is a bug that broken throttling, this patchset intends to improve the accuracy of throttling.

This patchset does two changes:
- Revert a specific patch to avoid broken throttling when

     max_samples_per_tick>1.

- Add throttling judgement when max_samples_per_tick=1.

base-commit: acc4d5ff0b61

Qing Wang (2):
Revert "perf/core: Fix hardlockup failure caused by perf throttle" perf/core: Fix broken throttling when max_samples_per_tick=1

  kernel/events/core.c | 17 ++++++++---------
  1 file changed, 8 insertions(+), 9 deletions(-)