Skip to contentSkip to conversation

perf/hw_breakpoint: fix ctx->mutex vs perf_event_mutex inversion on attr modify

PATCH In review Dominik Maier 3 participants 3 messages Nov 19 – Nov 20 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf/hw_breakpoint: fix ctx->mutex vs perf_event_mutex inversion on attr modify

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")
---
 kernel/events/hw_breakpoint.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)
kernel/events/hw_breakpoint.c +12 -19
index 7a60621..540c747 100644
@@ -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;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

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.

Quoted 7 lines

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")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Dominik Maier
Quoted 9 lines · 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.

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
Quoted 6 lines

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.

Quoted 6 lines

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

Quoted 2 lines
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.