Linux Perf Users
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Dominik Maier <dmnk@google.com>
Cc: Ingo Molnar <mingo@redhat.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Marco Elver <elver@google.com>, Jiri Olsa <jolsa@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Ian Rogers <irogers@google.com>,
	linux-perf-users@vger.kernel.org, dmnk+artist@google.com
Subject: Re: [PATCH] perf/hw_breakpoint: fix ctx->mutex vs perf_event_mutex inversion on attr modify
Date: Fri, 20 Nov 2026 12:56:01 +0100	[thread overview]
Message-ID: <20261120115601.GQ2668564@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20261119192228.2661266-1-dmnk@google.com>

On Thu, Nov 19, 2026 at 08:22:28PM +0100, Dominik Maier wrote:
> The perf events lock hierarchy documented in kernel/events/core.c orders
> task_struct::perf_event_mutex outside perf_event_context::mutex, which
> perf_event_open() and prctl(PR_TASK_PERF_EVENTS_ENABLE) follow.
> 
> However, perf_ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) acquires
> ctx->mutex first via perf_event_ctx_lock() and then calls
> perf_event_modify_attr() -> modify_user_hw_breakpoint_check() ->
> modify_bp_slot() -> bp_constraints_lock(), which locks
> bp->hw.target->perf_event_mutex when bp_type changes.

  perf_ioctl()
    perf_event_ctx_lock()                     // perf_event_context::mutex
    _perf_ioctl()
      MODIFY_ATTR:
        perf_event_modify_attr()
          mutex_lock(event->child_mutex)      // perf_event::child_mutex
          perf_event_modify_breakpoint()
            modify_user_hw_breakpoint_check()
              if (bp->attr.bp_type != attr->bp_type)
                modify_bp_slot()
                  bp_constraints_lock()       // task_struct::perf_event_mutex

> If one thread in a
> process calls prctl(PR_TASK_PERF_EVENTS_ENABLE) while another calls
> ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) with an alternating bp_type on a
> self-targeted breakpoint, the two threads deadlock against each other in
> TASK_UNINTERRUPTIBLE state.
> 
> Decouple bp_constraints_lock() from task_struct::perf_event_mutex by
> using a small static array of dedicated mutexes indexed by
> hash_ptr(tsk, BP_TASK_MUTEX_BITS). At most one bp_task_mutexes[] entry
> is ever held at a time, and ordering becomes strictly
> perf_event_mutex -> ctx->mutex -> bp_task_mutexes[] -> bp_cpuinfo_sem
> without extending task_struct.

How long until someone reports this as a scalability problem.. :/

> Fixes: 0912037fec11 ("perf/hw_breakpoint: Reduce contention with large number of tasks")
> Fixes: 32ff77e8cc9e ("perf/core: Implement fast breakpoint modification via _IOC_MODIFY_ATTRIBUTES")

I'm not sure about this second commit; that does introduce the
MODIFY_ATTR stuff, but things weren't broken back then.

If you really want a second commit, then 82aad7ff7ac2 seems like a fine
enough candidate, since I annotated that warning away without noticing
the inversion through modify_bp_slot().

The reason modify_bp_slot() exists is for switching between data and
code breakpoints; which I recon isn't something we can take away either.

Bah.

      parent reply	other threads:[~2026-11-20 11:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:22 [PATCH] perf/hw_breakpoint: fix ctx->mutex vs perf_event_mutex inversion on attr modify Dominik Maier
2026-11-19 19:31 ` sashiko-bot
2026-11-20 11:56 ` Peter Zijlstra [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120115601.GQ2668564@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=dmnk+artist@google.com \
    --cc=dmnk@google.com \
    --cc=elver@google.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox