Skip to content

Commit 6b8c512

Browse files
Dapeng Migregkh
authored andcommitted
perf/core: Fix system hang caused by cpu-clock usage
commit eb3182ef0405ff2f6668fd3e5ff9883f60ce8801 upstream. cpu-clock usage by the async-profiler tool can trigger a system hang, which got bisected back to the following commit by Octavia Togami: 18dbcbf ("perf: Fix the POLL_HUP delivery breakage") causes this issue The root cause of the hang is that cpu-clock is a special type of SW event which relies on hrtimers. The __perf_event_overflow() callback is invoked from the hrtimer handler for cpu-clock events, and __perf_event_overflow() tries to call cpu_clock_event_stop() to stop the event, which calls htimer_cancel() to cancel the hrtimer. But that's a recursion into the hrtimer code from a hrtimer handler, which (unsurprisingly) deadlocks. To fix this bug, use hrtimer_try_to_cancel() instead, and set the PERF_HES_STOPPED flag, which causes perf_swevent_hrtimer() to stop the event once it sees the PERF_HES_STOPPED flag. [ mingo: Fixed the comments and improved the changelog. ] Closes: https://lore.kernel.org/all/CAHPNGSQpXEopYreir+uDDEbtXTBvBvi8c6fYXJvceqtgTPao3Q@mail.gmail.com/ Fixes: 18dbcbf ("perf: Fix the POLL_HUP delivery breakage") Reported-by: Octavia Togami <[email protected]> Suggested-by: Peter Zijlstra <[email protected]> Signed-off-by: Dapeng Mi <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Tested-by: Octavia Togami <[email protected]> Cc: [email protected] Link: lucko/spark#530 Link: https://patch.msgid.link/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent bdb596c commit 6b8c512

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

kernel/events/core.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11757,7 +11757,8 @@ static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
1175711757

1175811758
event = container_of(hrtimer, struct perf_event, hw.hrtimer);
1175911759

11760-
if (event->state != PERF_EVENT_STATE_ACTIVE)
11760+
if (event->state != PERF_EVENT_STATE_ACTIVE ||
11761+
event->hw.state & PERF_HES_STOPPED)
1176111762
return HRTIMER_NORESTART;
1176211763

1176311764
event->pmu->read(event);
@@ -11803,15 +11804,20 @@ static void perf_swevent_cancel_hrtimer(struct perf_event *event)
1180311804
struct hw_perf_event *hwc = &event->hw;
1180411805

1180511806
/*
11806-
* The throttle can be triggered in the hrtimer handler.
11807-
* The HRTIMER_NORESTART should be used to stop the timer,
11808-
* rather than hrtimer_cancel(). See perf_swevent_hrtimer()
11807+
* Careful: this function can be triggered in the hrtimer handler,
11808+
* for cpu-clock events, so hrtimer_cancel() would cause a
11809+
* deadlock.
11810+
*
11811+
* So use hrtimer_try_to_cancel() to try to stop the hrtimer,
11812+
* and the cpu-clock handler also sets the PERF_HES_STOPPED flag,
11813+
* which guarantees that perf_swevent_hrtimer() will stop the
11814+
* hrtimer once it sees the PERF_HES_STOPPED flag.
1180911815
*/
1181011816
if (is_sampling_event(event) && (hwc->interrupts != MAX_INTERRUPTS)) {
1181111817
ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
1181211818
local64_set(&hwc->period_left, ktime_to_ns(remaining));
1181311819

11814-
hrtimer_cancel(&hwc->hrtimer);
11820+
hrtimer_try_to_cancel(&hwc->hrtimer);
1181511821
}
1181611822
}
1181711823

@@ -11855,12 +11861,14 @@ static void cpu_clock_event_update(struct perf_event *event)
1185511861

1185611862
static void cpu_clock_event_start(struct perf_event *event, int flags)
1185711863
{
11864+
event->hw.state = 0;
1185811865
local64_set(&event->hw.prev_count, local_clock());
1185911866
perf_swevent_start_hrtimer(event);
1186011867
}
1186111868

1186211869
static void cpu_clock_event_stop(struct perf_event *event, int flags)
1186311870
{
11871+
event->hw.state = PERF_HES_STOPPED;
1186411872
perf_swevent_cancel_hrtimer(event);
1186511873
if (flags & PERF_EF_UPDATE)
1186611874
cpu_clock_event_update(event);
@@ -11934,12 +11942,14 @@ static void task_clock_event_update(struct perf_event *event, u64 now)
1193411942

1193511943
static void task_clock_event_start(struct perf_event *event, int flags)
1193611944
{
11945+
event->hw.state = 0;
1193711946
local64_set(&event->hw.prev_count, event->ctx->time);
1193811947
perf_swevent_start_hrtimer(event);
1193911948
}
1194011949

1194111950
static void task_clock_event_stop(struct perf_event *event, int flags)
1194211951
{
11952+
event->hw.state = PERF_HES_STOPPED;
1194311953
perf_swevent_cancel_hrtimer(event);
1194411954
if (flags & PERF_EF_UPDATE)
1194511955
task_clock_event_update(event, event->ctx->time);

0 commit comments

Comments
 (0)