mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Jann Horn <jannh@google.com>,
	 linux-kernel@vger.kernel.org,
	 "Christian Brauner (Amutable)" <brauner@kernel.org>,
	stable@vger.kernel.org
Subject: [PATCH 1/2] perf: look up address filter files without ctx->mutex held
Date: Thu, 26 Nov 2026 12:45:37 +0100	[thread overview]
Message-ID: <20261126-work-exec-update_lock-leftovers-v1-1-1024b26792bc@kernel.org> (raw)
In-Reply-To: <20261126-work-exec-update_lock-leftovers-v1-0-1024b26792bc@kernel.org>

PERF_EVENT_IOC_SET_FILTER parses the address filters with the mutex of
the event's context held. A filter can name a file and the parser looks
that file up with kern_path(). So the mutex is held for as long as the
lookup takes and on a FUSE filesystem it's the caller who decides how
long that is.

Since commit 2e498d0a74e5 ("perf: Add support for event removal on
exec") perf_event_exec() takes that mutex with exec_update_lock held
whenever the task has a perf context. So an exec of the task the event
counts for hangs past the point of no return until the lookup is done
and can't be killed. And everyone who needs the exec_update_lock of the
task waits with it.

It gets worse. Since commit 6255da28d4bb ("proc: protect
ptrace_may_access() with exec_update_lock (FD links)") looking up
/proc/<pid>/exe takes the exec_update_lock of the task. So with such a
file in a filter the ioctl takes the two locks the other way round:

  WARNING: possible circular locking dependency detected
  ------------------------------------------------------
  perf-addr-filte/113 is trying to acquire lock:
  (&sig->exec_update_lock){++++}-{4:4}, at: call_proc_get_link+0x4f/0x110

  but task is already holding lock:
  (&ctx->mutex){+.+.}-{4:4}, at: perf_event_ctx_lock_nested+0xb2/0x200

         CPU0                    CPU1
         ----                    ----
    lock(&ctx->mutex);
                                 lock(&sig->exec_update_lock);
                                 lock(&ctx->mutex);
    rlock(&sig->exec_update_lock);

and an exec of the task deadlocks with it:

  task:exec-task       state:D pid:114
  Call Trace:
   __schedule+0x53e/0x13f0
   schedule+0x36/0x100
   schedule_preempt_disabled+0x11/0x20
   __mutex_lock+0x7bf/0x11f0
   perf_event_exec+0xb9/0x640
   begin_new_exec+0x622/0xf90
   load_elf_binary+0x12a/0x1990
   bprm_execve+0x39d/0x870
   do_execveat_common.isra.0+0x165/0x180
   __x64_sys_execve+0x38/0x50
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

  task:paf-setter      state:D pid:116
  Call Trace:
   __schedule+0x53e/0x13f0
   schedule+0x36/0x100
   schedule_preempt_disabled+0x11/0x20
   rwsem_down_read_slowpath+0x186/0x5f0
   down_read_killable+0x78/0x1b0
   call_proc_get_link+0x4f/0x110
   proc_pid_get_link+0x3c/0x70
   pick_link+0x52b/0x640
   step_into_slowpath+0x16a/0x3a0
   path_lookupat+0x169/0x2e0
   filename_lookup+0x124/0x230
   kern_path+0x2c/0x50
   _perf_ioctl+0xbd9/0xea0
   perf_ioctl+0x50/0x90
   __x64_sys_ioctl+0xb4/0x100
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

The parser doesn't need the mutex. So drop it while the filters are
parsed like perf_event_set_filter() already does for the filter of a
tracepoint. The pmu may be unregistered in the meantime so check again
whether the event has been revoked once the mutex is back.

The parser bumped the event's count of file based filters as it went
along and looked at event->ctx->task. Count in a local, store that when
the new filters are installed and test PERF_ATTACH_TASK instead.

This also makes the count exact. It was only ever reset when a set of
filters was refused. So once an event had a file based filter every
later set took the mmap lock of the task and for a task without an mm
the new ranges were never written.

Fixes: 2e498d0a74e5 ("perf: Add support for event removal on exec")
Fixes: 6255da28d4bb ("proc: protect ptrace_may_access() with exec_update_lock (FD links)")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 kernel/events/core.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index e870f12c3ad81..7ee954bc14dfb 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -12165,7 +12165,8 @@ static const match_table_t if_tokens = {
  */
 static int
 perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
-			     struct list_head *filters)
+			     struct list_head *filters,
+			     unsigned int *nr_file_filters)
 {
 	struct perf_addr_filter *filter = NULL;
 	char *start, *orig, *filename = NULL;
@@ -12277,7 +12278,7 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
 				 * different processes.
 				 */
 				ret = -EOPNOTSUPP;
-				if (!event->ctx->task)
+				if (!(READ_ONCE(event->attach_state) & PERF_ATTACH_TASK))
 					goto fail;
 
 				/* look up the path and grab its inode */
@@ -12292,7 +12293,7 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
 					     ->i_mode))
 					goto fail;
 
-				event->addr_filters.nr_file_filters++;
+				(*nr_file_filters)++;
 			}
 
 			/* ready to consume more filters */
@@ -12323,6 +12324,8 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
 static int
 perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
 {
+	struct perf_event_context *ctx = event->ctx;
+	unsigned int nr_file_filters = 0;
 	LIST_HEAD(filters);
 	int ret;
 
@@ -12330,14 +12333,30 @@ perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
 	 * Since this is called in perf_ioctl() path, we're already holding
 	 * ctx::mutex.
 	 */
-	lockdep_assert_held(&event->ctx->mutex);
+	lockdep_assert_held(&ctx->mutex);
 
 	if (WARN_ON_ONCE(event->parent))
 		return -EINVAL;
 
-	ret = perf_event_parse_addr_filter(event, filter_str, &filters);
+	/*
+	 * Parsing the filters looks up the files they refer to. That can
+	 * block for as long as the filesystem likes and a lookup below /proc
+	 * takes exec_update_lock which nests outside of ctx->mutex. So don't
+	 * hold ctx->mutex for it. As per perf_event_ctx_lock() we already
+	 * have a reference on ctx and an event with address filters isn't
+	 * moved to another context.
+	 */
+	mutex_unlock(&ctx->mutex);
+	ret = perf_event_parse_addr_filter(event, filter_str, &filters,
+					   &nr_file_filters);
+	mutex_lock(&ctx->mutex);
 	if (ret)
-		goto fail_clear_files;
+		return ret;
+
+	/* The pmu may have gone away while ctx->mutex was dropped. */
+	ret = -ENODEV;
+	if (event->state <= PERF_EVENT_STATE_REVOKED)
+		goto fail_free_filters;
 
 	ret = event->pmu->addr_filters_validate(&filters);
 	if (ret)
@@ -12345,6 +12364,7 @@ perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
 
 	/* remove existing filters, if any */
 	perf_addr_filters_splice(event, &filters);
+	event->addr_filters.nr_file_filters = nr_file_filters;
 
 	/* install new filters */
 	perf_event_for_each_child(event, perf_event_addr_filters_apply);
@@ -12353,10 +12373,6 @@ perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
 
 fail_free_filters:
 	free_filters_list(&filters);
-
-fail_clear_files:
-	event->addr_filters.nr_file_filters = 0;
-
 	return ret;
 }
 

-- 
2.53.0


  reply	other threads:[~2026-11-26 11:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 11:45 [PATCH 0/2] exec: two more sleepers in the way of exec Christian Brauner
2026-11-26 11:45 ` Christian Brauner [this message]
2026-11-26 11:45 ` [PATCH 2/2] proc: call task_vsize() without exec_update_lock held Christian Brauner

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=20261126-work-exec-update_lock-leftovers-v1-1-1024b26792bc@kernel.org \
    --to=brauner@kernel.org \
    --cc=acme@kernel.org \
    --cc=jannh@google.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=stable@vger.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

all inboxes | Powered by JetHome®