Re: [PATCH] perf: Fix deadlock in perf_mmap()

From: Qing Wang

Date: Mon Mar 09 2026 - 23:40:33 EST


On Tue, 10 Mar 2026 at 02:59, Ian Rogers <irogers@xxxxxxxxxx> wrote:
> Hi Qing, thank you for looking into this. I proposed a similar fix:
> https://lore.kernel.org/lkml/CAP-5=fW-wHEv=TCULwk_HVOhWHdqRd8AZoESZsU_vnhLjghUBQ@xxxxxxxxxxxxxx/
> but Haocheng noted it reintroduced the race condition the original fix
> was targeting. Haocheng has a larger fix in:
> https://lore.kernel.org/lkml/20260306093616.84299-1-yuhaocheng035@xxxxxxxxx/
> that should handle both the original race condition and the deadlock.

Oh, this fix is too large.

> I wonder that fix may be made smaller by passing a parameter like
> "holds_event_mmap_mutex" to perf_mmap_close, something like:
> ```
> static void __perf_mmap_close(struct vm_area_struct *vma, struct
> perf_event *event, bool holds_event_mmap_lock)
> {
> ... // code from original perf_mmap_close
> if ((!holds_event_mmap_lock &&
> !refcount_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
> ||
> (holds_event_mmap_lock && !refcount_dec_and_test(&event->mmap_count)))
> ...
>
> static void perf_mmap_close(struct vm_area_struct *vma)
> {
> struct perf_event *event = vma->vm_file->private_data;
> __perf_mmap_close(vma, event, /*holds_event_mmap_lock=*/false);
> }
>
> static void perf_mmap_close_locked(struct vm_area_struct *vma, struct
> perf_event *event)
> {
> __perf_mmap_close(vma, event, /*holds_event_mmap_lock=*/true);
> }
> ```
>
> Thanks,
> Ian

LGTM, your fix is better.

But, can you explain why there is still race issue after applying my patch?

Thanks,
Qing