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. ]
Unified · Split
| @@ -11773,7 +11773,8 @@ static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) · 11772 unmodified lines | ||
| 11773 | 11773 | |
| 11774 | 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 | 11778 | return HRTIMER_NORESTART; |
| 11778 | 11779 | |
| 11779 | 11780 | event->pmu->read(event); |
| @@ -11819,15 +11820,20 @@ static void perf_swevent_cancel_hrtimer(struct perf_event *event) · 39 unmodified lines | ||
| 11819 | 11820 | struct hw_perf_event *hwc = &event->hw; |
| 11820 | 11821 | |
| 11821 | 11822 | /* |
| 11822 | − * The throttle can be triggered in the hrtimer handler. | |
| 11823 | − * The HRTIMER_NORESTART should be used to stop the timer, | |
| 11824 | − * rather than hrtimer_cancel(). See perf_swevent_hrtimer() | |
| 11823 | + * Careful: this function can be triggered in the hrtimer handler, | |
| 11824 | + * for cpu-clock events, so hrtimer_cancel() would cause a | |
| 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 | 11832 | if (is_sampling_event(event) && (hwc->interrupts != MAX_INTERRUPTS)) { |
| 11827 | 11833 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
| 11828 | 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 | |
| 11872 | 11878 | static void cpu_clock_event_start(struct perf_event *event, int flags) |
| 11873 | 11879 | { |
| 11880 | + event->hw.state = 0; | |
| 11874 | 11881 | local64_set(&event->hw.prev_count, local_clock()); |
| 11875 | 11882 | perf_swevent_start_hrtimer(event); |
| 11876 | 11883 | } |
| 11877 | 11884 | |
| 11878 | 11885 | static void cpu_clock_event_stop(struct perf_event *event, int flags) |
| 11879 | 11886 | { |
| 11887 | + event->hw.state = PERF_HES_STOPPED; | |
| 11880 | 11888 | perf_swevent_cancel_hrtimer(event); |
| 11881 | 11889 | if (flags & PERF_EF_UPDATE) |
| 11882 | 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 | |
| 11951 | 11959 | static void task_clock_event_start(struct perf_event *event, int flags) |
| 11952 | 11960 | { |
| 11961 | + event->hw.state = 0; | |
| 11953 | 11962 | local64_set(&event->hw.prev_count, event->ctx->time); |
| 11954 | 11963 | perf_swevent_start_hrtimer(event); |
| 11955 | 11964 | } |
| 11956 | 11965 | |
| 11957 | 11966 | static void task_clock_event_stop(struct perf_event *event, int flags) |
| 11958 | 11967 | { |
| 11968 | + event->hw.state = PERF_HES_STOPPED; | |
| 11959 | 11969 | perf_swevent_cancel_hrtimer(event); |
| 11960 | 11970 | if (flags & PERF_EF_UPDATE) |
| 11961 | 11971 | task_clock_event_update(event, event->ctx->time); |