History

perf/core: Fix system hang caused by cpu-clock usage

cpu-clock usage by the async-profiler tool can trigger a system hang, which got bisected back to the following commit by Octavia Togami: 18dbcbfabfff ("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. ]

Dapeng Mi committed 1 year ago

Commit eb3182e1 parent 6146a0f1 file · +15 −5Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/events/core.c+15−5
@@ -11773,7 +11773,8 @@ static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) · 11772 unmodified lines
11773 11773
11774 event = container_of(hrtimer, struct perf_event, hw.hrtimer);11774 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
11775 11775
11776 if (event->state != PERF_EVENT_STATE_ACTIVE)11776 if (event->state != PERF_EVENT_STATE_ACTIVE ||
11777 event->hw.state & PERF_HES_STOPPED)
11777 return HRTIMER_NORESTART;11778 return HRTIMER_NORESTART;
11778 11779
11779 event->pmu->read(event);11780 event->pmu->read(event);
@@ -11819,15 +11820,20 @@ static void perf_swevent_cancel_hrtimer(struct perf_event *event) · 39 unmodified lines
11819 struct hw_perf_event *hwc = &event->hw;11820 struct hw_perf_event *hwc = &event->hw;
11820 11821
11821 /*11822 /*
11822 * The throttle can be triggered in the hrtimer handler.11823 * Careful: this function can be triggered in the hrtimer handler,
11823 * The HRTIMER_NORESTART should be used to stop the timer,11824 * for cpu-clock events, so hrtimer_cancel() would cause a
11824 * rather than hrtimer_cancel(). See perf_swevent_hrtimer()11825 * deadlock.
11826 *
11827 * So use hrtimer_try_to_cancel() to try to stop the hrtimer,
11828 * and the cpu-clock handler also sets the PERF_HES_STOPPED flag,
11829 * which guarantees that perf_swevent_hrtimer() will stop the
11830 * hrtimer once it sees the PERF_HES_STOPPED flag.
11825 */11831 */
11826 if (is_sampling_event(event) && (hwc->interrupts != MAX_INTERRUPTS)) {11832 if (is_sampling_event(event) && (hwc->interrupts != MAX_INTERRUPTS)) {
11827 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);11833 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
11828 local64_set(&hwc->period_left, ktime_to_ns(remaining));11834 local64_set(&hwc->period_left, ktime_to_ns(remaining));
11829 11835
11830 hrtimer_cancel(&hwc->hrtimer);11836 hrtimer_try_to_cancel(&hwc->hrtimer);
11831 }11837 }
11832}11838}
11833 11839
@@ -11871,12 +11877,14 @@ static void cpu_clock_event_update(struct perf_event *event) · 37 unmodified lines
11871 11877
11872static void cpu_clock_event_start(struct perf_event *event, int flags)11878static void cpu_clock_event_start(struct perf_event *event, int flags)
11873{11879{
11880 event->hw.state = 0;
11874 local64_set(&event->hw.prev_count, local_clock());11881 local64_set(&event->hw.prev_count, local_clock());
11875 perf_swevent_start_hrtimer(event);11882 perf_swevent_start_hrtimer(event);
11876}11883}
11877 11884
11878static void cpu_clock_event_stop(struct perf_event *event, int flags)11885static void cpu_clock_event_stop(struct perf_event *event, int flags)
11879{11886{
11887 event->hw.state = PERF_HES_STOPPED;
11880 perf_swevent_cancel_hrtimer(event);11888 perf_swevent_cancel_hrtimer(event);
11881 if (flags & PERF_EF_UPDATE)11889 if (flags & PERF_EF_UPDATE)
11882 cpu_clock_event_update(event);11890 cpu_clock_event_update(event);
@@ -11950,12 +11958,14 @@ static void task_clock_event_update(struct perf_event *event, u64 now) · 67 unmodified lines
11950 11958
11951static void task_clock_event_start(struct perf_event *event, int flags)11959static void task_clock_event_start(struct perf_event *event, int flags)
11952{11960{
11961 event->hw.state = 0;
11953 local64_set(&event->hw.prev_count, event->ctx->time);11962 local64_set(&event->hw.prev_count, event->ctx->time);
11954 perf_swevent_start_hrtimer(event);11963 perf_swevent_start_hrtimer(event);
11955}11964}
11956 11965
11957static void task_clock_event_stop(struct perf_event *event, int flags)11966static void task_clock_event_stop(struct perf_event *event, int flags)
11958{11967{
11968 event->hw.state = PERF_HES_STOPPED;
11959 perf_swevent_cancel_hrtimer(event);11969 perf_swevent_cancel_hrtimer(event);
11960 if (flags & PERF_EF_UPDATE)11970 if (flags & PERF_EF_UPDATE)
11961 task_clock_event_update(event, event->ctx->time);11971 task_clock_event_update(event, event->ctx->time);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t