Linux Perf Users
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Namhyung Kim <namhyung@kernel.org>
Cc: 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,
	 Dominik Maier <dmnk@google.com>
Subject: [PATCH] perf/hw_breakpoint: fix ctx->mutex vs perf_event_mutex inversion on attr modify
Date: Thu, 19 Nov 2026 20:22:28 +0100	[thread overview]
Message-ID: <20261119192228.2661266-1-dmnk@google.com> (raw)

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. 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.

This issue was discovered with an AI-assisted code review tool (ARTIST)
and reproduced in QEMU/KVM on an unmodified v7.4-rc3 kernel without
kernel modifications or error injection; a standalone reproducer is
available upon maintainers' request. The fix was compile-tested with
-Werror and checked with checkpatch.pl --strict; an equivalent fix was
verified against the reproducer in QEMU/KVM.

Fixes: 0912037fec11 ("perf/hw_breakpoint: Reduce contention with large number of tasks")
Fixes: 32ff77e8cc9e ("perf/core: Implement fast breakpoint modification via _IOC_MODIFY_ATTRIBUTES")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
 kernel/events/hw_breakpoint.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)

diff --git a/kernel/events/hw_breakpoint.c b/kernel/events/hw_breakpoint.c
index 7a60621..540c747 100644
--- a/kernel/events/hw_breakpoint.c
+++ b/kernel/events/hw_breakpoint.c
@@ -23,6 +23,7 @@
 #include <linux/bug.h>
 #include <linux/cpu.h>
 #include <linux/export.h>
+#include <linux/hash.h>
 #include <linux/init.h>
 #include <linux/irqflags.h>
 #include <linux/kdebug.h>
@@ -94,22 +95,21 @@ static bool constraints_initialized __ro_after_init;
  */
 DEFINE_STATIC_PERCPU_RWSEM(bp_cpuinfo_sem);
 
+#define BP_TASK_MUTEX_BITS	6
+static struct mutex bp_task_mutexes[1 << BP_TASK_MUTEX_BITS];
+
 /*
  * Return mutex to serialize accesses to per-task lists in task_bps_ht. Since
  * rhltable synchronizes concurrent insertions/deletions, independent tasks may
- * insert/delete concurrently; therefore, a mutex per task is sufficient.
- *
- * Uses task_struct::perf_event_mutex, to avoid extending task_struct with a
- * hw_breakpoint-only mutex, which may be infrequently used. The caveat here is
- * that hw_breakpoint may contend with per-task perf event list management. The
- * assumption is that perf usecases involving hw_breakpoints are very unlikely
- * to result in unnecessary contention.
+ * insert/delete concurrently; therefore, a hashed mutex pool indexed by task
+ * pointer is sufficient without extending task_struct or coupling lock ordering
+ * with task_struct::perf_event_mutex and perf_event_context::mutex.
  */
 static inline struct mutex *get_task_bps_mutex(struct perf_event *bp)
 {
 	struct task_struct *tsk = bp->hw.target;
 
-	return tsk ? &tsk->perf_event_mutex : NULL;
+	return tsk ? &bp_task_mutexes[hash_ptr(tsk, BP_TASK_MUTEX_BITS)] : NULL;
 }
 
 static struct mutex *bp_constraints_lock(struct perf_event *bp)
@@ -117,17 +117,7 @@ static struct mutex *bp_constraints_lock(struct perf_event *bp)
 	struct mutex *tsk_mtx = get_task_bps_mutex(bp);
 
 	if (tsk_mtx) {
-		/*
-		 * Fully analogous to the perf_try_init_event() nesting
-		 * argument in the comment near perf_event_ctx_lock_nested();
-		 * this child->perf_event_mutex cannot ever deadlock against
-		 * the parent->perf_event_mutex usage from
-		 * perf_event_task_{en,dis}able().
-		 *
-		 * Specifically, inherited events will never occur on
-		 * ->perf_event_list.
-		 */
-		mutex_lock_nested(tsk_mtx, SINGLE_DEPTH_NESTING);
+		mutex_lock(tsk_mtx);
 		percpu_down_read(&bp_cpuinfo_sem);
 	} else {
 		percpu_down_write(&bp_cpuinfo_sem);
@@ -1008,6 +998,9 @@ int __init init_hw_breakpoint(void)
 {
 	int ret;
 
+	for (int i = 0; i < ARRAY_SIZE(bp_task_mutexes); i++)
+		mutex_init(&bp_task_mutexes[i]);
+
 	ret = rhltable_init(&task_bps_ht, &task_bps_ht_params);
 	if (ret)
 		return ret;
-- 
2.56.1.750.gf70dc682cf-goog


             reply	other threads:[~2026-11-19 19:22 UTC|newest]

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

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=20261119192228.2661266-1-dmnk@google.com \
    --to=dmnk@google.com \
    --cc=acme@kernel.org \
    --cc=dmnk+artist@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 \
    --cc=peterz@infradead.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