* [PATCH] perf/hw_breakpoint: fix ctx->mutex vs perf_event_mutex inversion on attr modify
@ 2026-11-19 19:22 Dominik Maier
2026-11-19 19:31 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Dominik Maier @ 2026-11-19 19:22 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Namhyung Kim
Cc: Arnaldo Carvalho de Melo, Marco Elver, Jiri Olsa, Mark Rutland,
Ian Rogers, linux-perf-users, dmnk+artist, Dominik Maier
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
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] perf/hw_breakpoint: fix ctx->mutex vs perf_event_mutex inversion on attr modify
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
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-19 19:31 UTC (permalink / raw)
To: Dominik Maier; +Cc: linux-perf-users
> 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
> [ ... ]
>
> 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>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119192228.2661266-1-dmnk@google.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-19 19:31 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox