Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH 0/2] exec: two more sleepers in the way of exec
@ 2026-11-26 11:45 Christian Brauner
  2026-11-26 11:45 ` [PATCH 1/2] perf: look up address filter files without ctx->mutex held Christian Brauner
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Christian Brauner @ 2026-11-26 11:45 UTC (permalink / raw)
  To: linux-perf-users, linux-fsdevel
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Jann Horn,
	linux-kernel, Christian Brauner (Amutable), stable

This fixes two more places where a task sleeps on something userspace
controls with a lock held that exec needs:

* PERF_EVENT_IOC_SET_FILTER looks up the files of address filters with
  the mutex of the perf context held and perf_event_exec() takes that
  mutex with exec_update_lock held. With a file like /proc/<pid>/exe the
  lookup takes exec_update_lock itself and deadlocks with exec.

* Without an MMU a read of /proc/<pid>/stat waits for the mmap lock with
  exec_update_lock held.

I don't have a machine with intel_pt at hand. The perf patch was tested
with a hack that gives the task clock address filters.

What's left in that ioctl is that it takes the mmap lock of the task
with the mutex held to find the mappings of the files.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (2):
      perf: look up address filter files without ctx->mutex held
      proc: call task_vsize() without exec_update_lock held

 fs/proc/array.c      | 13 +++++++++++--
 kernel/events/core.c | 36 ++++++++++++++++++++++++++----------
 2 files changed, 37 insertions(+), 12 deletions(-)
---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261125-work-exec-update_lock-leftovers-6778785ed04a


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 1/2] perf: look up address filter files without ctx->mutex held
  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
  2026-11-26 15:24   ` Peter Zijlstra
  2026-11-26 11:45 ` [PATCH 2/2] proc: call task_vsize() without exec_update_lock held Christian Brauner
  2026-11-26 11:54 ` [PATCH 0/2] exec: two more sleepers in the way of exec sashiko-bot
  2 siblings, 1 reply; 12+ messages in thread
From: Christian Brauner @ 2026-11-26 11:45 UTC (permalink / raw)
  To: linux-perf-users, linux-fsdevel
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Jann Horn,
	linux-kernel, Christian Brauner (Amutable), stable

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


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 2/2] proc: call task_vsize() without exec_update_lock held
  2026-11-26 11:45 [PATCH 0/2] exec: two more sleepers in the way of exec Christian Brauner
  2026-11-26 11:45 ` [PATCH 1/2] perf: look up address filter files without ctx->mutex held Christian Brauner
@ 2026-11-26 11:45 ` Christian Brauner
  2026-11-26 11:54 ` [PATCH 0/2] exec: two more sleepers in the way of exec sashiko-bot
  2 siblings, 0 replies; 12+ messages in thread
From: Christian Brauner @ 2026-11-26 11:45 UTC (permalink / raw)
  To: linux-perf-users, linux-fsdevel
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Jann Horn,
	linux-kernel, Christian Brauner (Amutable)

Since commit 6650527444da ("proc: protect ptrace_may_access() with
exec_update_lock (part 1)") do_task_stat() holds the exec_update_lock of
the task from the access check until the last field is printed. In
between it calls task_vsize() and without an MMU that takes the mmap
lock to add up the vmas:

  locks held by stat-reader/46:
   #0: (&p->lock){+.+.}-{4:4}, at: seq_read_iter+0x38/0x32a
   #1: (&sig->exec_update_lock){++++}-{4:4}, at: do_task_stat+0x50/0xd04
   #2: (&mm->mmap_lock){++++}-{4:4}, at: task_vsize+0x5e/0xd2

So a read of /proc/<pid>/stat waits for the mmap lock with
exec_update_lock held. And without an MMU the mmap lock can be held for
a long time. A private mapping of a file is read into memory with it
held for writing:

  locks held by holder/47:
   #0: (&mm->mmap_lock){++++}-{4:4}, at: vm_mmap_pgoff+0x56/0x136
   #1: (nommu_region_sem){+.+.}-{4:4}, at: do_mmap+0x21e/0xb02
   #2: (&sb->s_type->i_mutex_key#7){.+.+}-{4:4}, at: blkdev_read_iter+0xa6/0x1c0

The lock is only needed for the access check and for what is read from
the task depending on it. The mm is pinned and everything else is shown
to everyone anyway. So drop the lock once that is done and call
task_vsize() afterwards.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/proc/array.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/fs/proc/array.c b/fs/proc/array.c
index f6f75d206762a..3ff6f2455e5e4 100644
--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -493,7 +493,6 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 	permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
 	mm = get_task_mm(task);
 	if (mm) {
-		vsize = task_vsize(mm);
 		/*
 		 * esp and eip are intentionally zeroed out.  There is no
 		 * non-racy way to read them without freezing the task.
@@ -543,6 +542,17 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 	if (permitted && (!whole || num_threads < 2))
 		wchan = !task_is_running(task);
 
+	/*
+	 * What depends on the access check has been read from the task or
+	 * comes from the mm that was pinned along with it. Don't hold
+	 * exec_update_lock for the rest, task_vsize() takes the mmap lock
+	 * without an MMU.
+	 */
+	up_read(&task->signal->exec_update_lock);
+
+	if (mm)
+		vsize = task_vsize(mm);
+
 	scoped_guard(rcu) {
 		scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) {
 			cmin_flt = sig->cmin_flt;
@@ -662,7 +672,6 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 		seq_puts(m, " 0");
 
 	seq_putc(m, '\n');
-	up_read(&task->signal->exec_update_lock);
 	if (mm)
 		mmput(mm);
 	return 0;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/2] exec: two more sleepers in the way of exec
  2026-11-26 11:45 [PATCH 0/2] exec: two more sleepers in the way of exec Christian Brauner
  2026-11-26 11:45 ` [PATCH 1/2] perf: look up address filter files without ctx->mutex held Christian Brauner
  2026-11-26 11:45 ` [PATCH 2/2] proc: call task_vsize() without exec_update_lock held Christian Brauner
@ 2026-11-26 11:54 ` sashiko-bot
  2 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-26 11:54 UTC (permalink / raw)
  To: Christian Brauner, linux-perf-users

Sashiko has reviewed this 2-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126-work-exec-update_lock-leftovers-v1-0-1024b26792bc@kernel.org

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/2] perf: look up address filter files without ctx->mutex held
  2026-11-26 11:45 ` [PATCH 1/2] perf: look up address filter files without ctx->mutex held Christian Brauner
@ 2026-11-26 15:24   ` Peter Zijlstra
  2026-11-26 20:56     ` Christian Brauner
  0 siblings, 1 reply; 12+ messages in thread
From: Peter Zijlstra @ 2026-11-26 15:24 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-perf-users, linux-fsdevel, Ingo Molnar,
	Arnaldo Carvalho de Melo, Jann Horn, linux-kernel, stable

On Thu, Nov 26, 2026 at 12:45:37PM +0100, Christian Brauner wrote:
> 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.

Nah, we do this per commit:

79c9ce57eb2d ("perf/core: Fix perf_event_open() vs. execve() race").
6914303824bb ("perf: Use new infrastructure to fix deadlocks in execve")
f7cfd871ae0c ("exec: Transform exec_update_mutex into a rw_semaphore")

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

Somewhat hard to read; and I'm not sure I follow. You're saying:

  ioctl(PERF_EVENT_IOC_SET_FILTER)
    perf_ioctl()
      perf_event_ctx_lock()		// perf_event_context::mutex
      _perf_ioctl()
        perf_event_set_filter()
	  perf_event_set_addr_filter()
	    perf_event_parse_addr_filter()
	      kern_path()
	        ...
		  call_proc_get_link()
		    down_read_killable(&exec_update_lock)

This violates established locking order; which is correct.

(Why can we add proc files to the filter?)

But then:

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

I'm not sure about this. That is, it is probably true with the current
drivers (intel_pt, coresight and some pending risc-v thing) but in
general there really isn't anything that stops an event from being
migrated.

But there's worse; the ctx->lock is also the thing that serializes
ioctl(), dropping it can get you another ioctl going on the same event,
and you can end up doing nested SET_FILTER for example.

And yes, perf_event_set_filter() drops ctx->mutex for the tracepoint
crud, but that has its own internal locking and never ever uses ctx
again afterwards.

I would really rather exclude procfs if at all possible.

> +	 */
> +	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
> 

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/2] perf: look up address filter files without ctx->mutex held
  2026-11-26 15:24   ` Peter Zijlstra
@ 2026-11-26 20:56     ` Christian Brauner
  2026-11-26 21:26       ` Christian Brauner
  0 siblings, 1 reply; 12+ messages in thread
From: Christian Brauner @ 2026-11-26 20:56 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: linux-perf-users, linux-fsdevel, Ingo Molnar,
	Arnaldo Carvalho de Melo, Jann Horn, linux-kernel, stable

On Thu, Nov 26, 2026 at 04:24:14PM +0100, Peter Zijlstra wrote:
> On Thu, Nov 26, 2026 at 12:45:37PM +0100, Christian Brauner wrote:
> > 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.
> 
> Nah, we do this per commit:
> 
> 79c9ce57eb2d ("perf/core: Fix perf_event_open() vs. execve() race").
> 6914303824bb ("perf: Use new infrastructure to fix deadlocks in execve")
> f7cfd871ae0c ("exec: Transform exec_update_mutex into a rw_semaphore")
> 
> > 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>
> 
> Somewhat hard to read; and I'm not sure I follow. You're saying:
> 
>   ioctl(PERF_EVENT_IOC_SET_FILTER)
>     perf_ioctl()
>       perf_event_ctx_lock()		// perf_event_context::mutex
>       _perf_ioctl()
>         perf_event_set_filter()
> 	  perf_event_set_addr_filter()
> 	    perf_event_parse_addr_filter()
> 	      kern_path()
> 	        ...
> 		  call_proc_get_link()
> 		    down_read_killable(&exec_update_lock)

Yes.

> This violates established locking order; which is correct.
> 
> (Why can we add proc files to the filter?)

I think we can. But it feels a bit annoying because this such lock
inversions involving exec_update_lock and perf isn't the first time.
So I tried to get this inversion out of the way.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/2] perf: look up address filter files without ctx->mutex held
  2026-11-26 20:56     ` Christian Brauner
@ 2026-11-26 21:26       ` Christian Brauner
  2026-11-26 21:59         ` Christian Brauner
  2026-11-27  8:13         ` Peter Zijlstra
  0 siblings, 2 replies; 12+ messages in thread
From: Christian Brauner @ 2026-11-26 21:26 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: linux-perf-users, linux-fsdevel, Ingo Molnar,
	Arnaldo Carvalho de Melo, Jann Horn, linux-kernel, stable

On Thu, Nov 26, 2026 at 09:56:37PM +0100, Christian Brauner wrote:
> On Thu, Nov 26, 2026 at 04:24:14PM +0100, Peter Zijlstra wrote:
> > (Why can we add proc files to the filter?)
>
> I think we can. But it feels a bit annoying because this such lock
> inversions involving exec_update_lock and perf isn't the first time.
> So I tried to get this inversion out of the way.

So I looked at this. It doesn't work. The filter never contains a procfs
file. It's just that kern_path() follows /proc/<pid>/exe to the binary
and the parser wants a regular file. IOW, procfs is only walked through
and exec_update_lock is then taken in the middle of that walk:

kern_path()
-> ...
   -> pick_link()
      -> proc_pid_get_link()
         -> call_proc_get_link()
            -> down_read_killable(&exec_update_lock)
         -> nd_jump_link()        // checks LOOKUP_NO_MAGICLINKS

Same for cwd, root, fd/<n> and map_files/, and proc_ns_get_link() does
it for ns/. So by the time kern_path() returns the lock has been taken
and dropped again. WE can't catch that.

procfs itself would have to look at the lookup flags before it takes
exec_update_lock. IOW, path lookup would have to work around perf's
lock ordering. And that only takes care of procfs. On FUSE the lookup
still blocks for as long as the server wants with ctx->mutex held and
exec waits for that.

So ok, if we can't drop ctx->mutex in the middle of the ioctl then how
about we don't hold ctx->mutex during parsing to begin with?
We let perf_ioctl() hand PERF_EVENT_IOC_SET_FILTER off before it takes
the mutex:

ioctl(PERF_EVENT_IOC_SET_FILTER)
-> perf_ioctl()
   -> perf_event_set_filter()                // no ctx->mutex
      -> perf_event_set_addr_filter()
         -> perf_event_parse_addr_filter()   // kern_path(), own list
         -> perf_event_ctx_lock()
            // revoked? has_addr_filter()? ->addr_filters_validate()
            -> perf_addr_filters_splice()
            -> perf_event_addr_filters_apply() on each child
         -> perf_event_ctx_unlock()

The parser fills its own list and counts the file filters in locals.
Everything that needs ctx->mutex then happens in one go under
perf_event_ctx_lock().

So ctx->mutex is never dropped in the middle of an ioctl, a migrated
event is handled by perf_event_ctx_lock() like everywhere else and no
ctx is carried across the lookup?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/2] perf: look up address filter files without ctx->mutex held
  2026-11-26 21:26       ` Christian Brauner
@ 2026-11-26 21:59         ` Christian Brauner
  2026-11-27  8:13         ` Peter Zijlstra
  1 sibling, 0 replies; 12+ messages in thread
From: Christian Brauner @ 2026-11-26 21:59 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: linux-perf-users, linux-fsdevel, Ingo Molnar,
	Arnaldo Carvalho de Melo, Jann Horn, linux-kernel, stable

On Thu, Nov 26, 2026 at 10:26:02PM +0100, Christian Brauner wrote:
> On Thu, Nov 26, 2026 at 09:56:37PM +0100, Christian Brauner wrote:
> > On Thu, Nov 26, 2026 at 04:24:14PM +0100, Peter Zijlstra wrote:
> > > (Why can we add proc files to the filter?)
> >
> > I think we can. But it feels a bit annoying because this such lock
> > inversions involving exec_update_lock and perf isn't the first time.
> > So I tried to get this inversion out of the way.
> 
> So I looked at this. It doesn't work. The filter never contains a procfs
> file. It's just that kern_path() follows /proc/<pid>/exe to the binary
> and the parser wants a regular file. IOW, procfs is only walked through
> and exec_update_lock is then taken in the middle of that walk:
> 
> kern_path()
> -> ...
>    -> pick_link()
>       -> proc_pid_get_link()
>          -> call_proc_get_link()
>             -> down_read_killable(&exec_update_lock)
>          -> nd_jump_link()        // checks LOOKUP_NO_MAGICLINKS
> 
> Same for cwd, root, fd/<n> and map_files/, and proc_ns_get_link() does
> it for ns/. So by the time kern_path() returns the lock has been taken
> and dropped again. WE can't catch that.
> 
> procfs itself would have to look at the lookup flags before it takes
> exec_update_lock. IOW, path lookup would have to work around perf's
> lock ordering. And that only takes care of procfs. On FUSE the lookup
> still blocks for as long as the server wants with ctx->mutex held and
> exec waits for that.
> 
> So ok, if we can't drop ctx->mutex in the middle of the ioctl then how
> about we don't hold ctx->mutex during parsing to begin with?
> We let perf_ioctl() hand PERF_EVENT_IOC_SET_FILTER off before it takes
> the mutex:
> 
> ioctl(PERF_EVENT_IOC_SET_FILTER)
> -> perf_ioctl()
>    -> perf_event_set_filter()                // no ctx->mutex
>       -> perf_event_set_addr_filter()
>          -> perf_event_parse_addr_filter()   // kern_path(), own list
>          -> perf_event_ctx_lock()
>             // revoked? has_addr_filter()? ->addr_filters_validate()
>             -> perf_addr_filters_splice()
>             -> perf_event_addr_filters_apply() on each child
>          -> perf_event_ctx_unlock()
> 
> The parser fills its own list and counts the file filters in locals.
> Everything that needs ctx->mutex then happens in one go under
> perf_event_ctx_lock().
> 
> So ctx->mutex is never dropped in the middle of an ioctl, a migrated
> event is handled by perf_event_ctx_lock() like everywhere else and no
> ctx is carried across the lookup?

Just as a general final comment: holding any global lock over a whole
path lookup is a not a good idea. We've seen that over and over again.
So if we can get rid of that, we should.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/2] perf: look up address filter files without ctx->mutex held
  2026-11-26 21:26       ` Christian Brauner
  2026-11-26 21:59         ` Christian Brauner
@ 2026-11-27  8:13         ` Peter Zijlstra
  2026-11-27  9:18           ` Peter Zijlstra
  1 sibling, 1 reply; 12+ messages in thread
From: Peter Zijlstra @ 2026-11-27  8:13 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-perf-users, linux-fsdevel, Ingo Molnar,
	Arnaldo Carvalho de Melo, Jann Horn, linux-kernel, stable

On Thu, Nov 26, 2026 at 10:26:02PM +0100, Christian Brauner wrote:
> So ok, if we can't drop ctx->mutex in the middle of the ioctl then how
> about we don't hold ctx->mutex during parsing to begin with?
> We let perf_ioctl() hand PERF_EVENT_IOC_SET_FILTER off before it takes
> the mutex:
> 
> ioctl(PERF_EVENT_IOC_SET_FILTER)
> -> perf_ioctl()
>    -> perf_event_set_filter()                // no ctx->mutex
>       -> perf_event_set_addr_filter()
>          -> perf_event_parse_addr_filter()   // kern_path(), own list
>          -> perf_event_ctx_lock()
>             // revoked? has_addr_filter()? ->addr_filters_validate()
>             -> perf_addr_filters_splice()
>             -> perf_event_addr_filters_apply() on each child
>          -> perf_event_ctx_unlock()
> 
> The parser fills its own list and counts the file filters in locals.
> Everything that needs ctx->mutex then happens in one go under
> perf_event_ctx_lock().
> 
> So ctx->mutex is never dropped in the middle of an ioctl, a migrated
> event is handled by perf_event_ctx_lock() like everywhere else and no
> ctx is carried across the lookup?

Let me try this.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/2] perf: look up address filter files without ctx->mutex held
  2026-11-27  8:13         ` Peter Zijlstra
@ 2026-11-27  9:18           ` Peter Zijlstra
  2026-11-27  9:45             ` Christian Brauner
  0 siblings, 1 reply; 12+ messages in thread
From: Peter Zijlstra @ 2026-11-27  9:18 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-perf-users, linux-fsdevel, Ingo Molnar,
	Arnaldo Carvalho de Melo, Jann Horn, linux-kernel, stable,
	adrian.hunter, alexander.shishkin, dapeng1.mi

On Fri, Nov 27, 2026 at 09:13:03AM +0100, Peter Zijlstra wrote:
> On Thu, Nov 26, 2026 at 10:26:02PM +0100, Christian Brauner wrote:
> > So ok, if we can't drop ctx->mutex in the middle of the ioctl then how
> > about we don't hold ctx->mutex during parsing to begin with?
> > We let perf_ioctl() hand PERF_EVENT_IOC_SET_FILTER off before it takes
> > the mutex:
> > 
> > ioctl(PERF_EVENT_IOC_SET_FILTER)
> > -> perf_ioctl()
> >    -> perf_event_set_filter()                // no ctx->mutex
> >       -> perf_event_set_addr_filter()
> >          -> perf_event_parse_addr_filter()   // kern_path(), own list
> >          -> perf_event_ctx_lock()
> >             // revoked? has_addr_filter()? ->addr_filters_validate()
> >             -> perf_addr_filters_splice()
> >             -> perf_event_addr_filters_apply() on each child
> >          -> perf_event_ctx_unlock()
> > 
> > The parser fills its own list and counts the file filters in locals.
> > Everything that needs ctx->mutex then happens in one go under
> > perf_event_ctx_lock().
> > 
> > So ctx->mutex is never dropped in the middle of an ioctl, a migrated
> > event is handled by perf_event_ctx_lock() like everywhere else and no
> > ctx is carried across the lookup?
> 
> Let me try this.

Compile tested only; Alexander, Adrian; could any of you take a hard
look at this and actually test?

---
 kernel/events/core.c | 135 +++++++++++++++++++++++++--------------------------
 1 file changed, 67 insertions(+), 68 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 77c501005143..c019e4faf655 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6632,16 +6632,24 @@ static inline bool is_perf_file(struct fd f)
 
 static int perf_event_set_output(struct perf_event *event,
 				 struct perf_event *output_event);
-static int perf_event_set_filter(struct perf_event *event, void __user *arg);
+static int perf_event_set_filter(struct perf_event *event, struct list_head *filters);
 static int perf_copy_attr(struct perf_event_attr __user *uattr,
 			  struct perf_event_attr *attr);
 static int __perf_event_set_bpf_prog(struct perf_event *event,
 				     struct bpf_prog *prog,
 				     u64 bpf_cookie);
 
-static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
+struct perf_ioctl_ctx {
+	unsigned int cmd;
+	unsigned long arg;
+	struct list_head filters;
+};
+
+static long _perf_ioctl(struct perf_event *event, struct perf_ioctl_ctx *ctx)
 {
 	void (*func)(struct perf_event *);
+	unsigned long arg = ctx->arg;
+	unsigned int cmd = ctx->cmd;
 	u32 flags = arg;
 
 	if (event->state <= PERF_EVENT_STATE_REVOKED)
@@ -6692,7 +6700,7 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
 	}
 
 	case PERF_EVENT_IOC_SET_FILTER:
-		return perf_event_set_filter(event, (void __user *)arg);
+		return perf_event_set_filter(event, &ctx->filters);
 
 	case PERF_EVENT_IOC_SET_BPF:
 	{
@@ -6751,10 +6759,21 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
 	return 0;
 }
 
+static inline bool perf_event_is_tracing(struct perf_event *event);
+static void free_filters_list(struct list_head *filters);
+static int
+perf_event_parse_addr_filter(int event_node, bool event_task, char *fstr,
+			     struct list_head *filters);
+
 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
 	struct perf_event *event = file->private_data;
 	struct perf_event_context *ctx;
+	struct perf_ioctl_ctx ic = {
+		.cmd = cmd,
+		.arg = arg,
+		.filters = LIST_HEAD_INIT(ic.filters),
+	};
 	long ret;
 
 	/* Treat ioctl like writes as it is likely a mutating operation. */
@@ -6762,10 +6781,34 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	if (ret)
 		return ret;
 
+	if (cmd == PERF_EVENT_IOC_SET_FILTER) {
+		int cpu = READ_ONCE(event->cpu);
+		int node = cpu == -1 ? NUMA_NO_NODE : cpu_to_node(cpu);
+		bool task = event->attach_state & PERF_ATTACH_TASK;
+		char *filter_str __free(kfree) =
+			strndup_user((void __user *)arg, PAGE_SIZE);
+
+		if (IS_ERR(filter_str))
+			return PTR_ERR(filter_str);
+
+#ifdef CONFIG_EVENT_TRACING
+		if (perf_event_is_tracing(event)) {
+			return ftrace_profile_set_filter(event, event->attr.config,
+							filter_str);
+		}
+#endif
+		ret = perf_event_parse_addr_filter(node, task, filter_str,
+						   &ic.filters);
+		if (ret)
+			return ret;
+	}
+
 	ctx = perf_event_ctx_lock(event);
-	ret = _perf_ioctl(event, cmd, arg);
+	ret = _perf_ioctl(event, &ic);
 	perf_event_ctx_unlock(event, ctx);
 
+	free_filters_list(&ic.filters);
+
 	return ret;
 }
 
@@ -11990,9 +12033,8 @@ void perf_bp_event(struct perf_event *bp, void *data)
  * Allocate a new address filter
  */
 static struct perf_addr_filter *
-perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
+perf_addr_filter_new(int node, struct list_head *filters)
 {
-	int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
 	struct perf_addr_filter *filter;
 
 	filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
@@ -12035,8 +12077,17 @@ static void perf_addr_filters_splice(struct perf_event *event,
 	raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
 
 	list_splice_init(&event->addr_filters.list, &list);
-	if (head)
+	event->addr_filters.nr_file_filters = 0;
+
+	if (head) {
+		struct perf_addr_filter *filter;
+
 		list_splice(head, &event->addr_filters.list);
+		list_for_each_entry(filter, &event->addr_filters.list, entry) {
+			if (filter->path.dentry)
+				event->addr_filters.nr_file_filters++;
+		}
+	}
 
 	raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
 
@@ -12186,7 +12237,7 @@ static const match_table_t if_tokens = {
  * Address filter string parser
  */
 static int
-perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
+perf_event_parse_addr_filter(int event_node, bool event_task, char *fstr,
 			     struct list_head *filters)
 {
 	struct perf_addr_filter *filter = NULL;
@@ -12213,7 +12264,7 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
 
 		/* filter definition begins */
 		if (state == IF_STATE_ACTION) {
-			filter = perf_addr_filter_new(event, filters);
+			filter = perf_addr_filter_new(event_node, filters);
 			if (!filter)
 				goto fail;
 		}
@@ -12299,7 +12350,7 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
 				 * different processes.
 				 */
 				ret = -EOPNOTSUPP;
-				if (!event->ctx->task)
+				if (!event_task)
 					goto fail;
 
 				/* look up the path and grab its inode */
@@ -12310,11 +12361,8 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
 
 				ret = -EINVAL;
 				if (!filter->path.dentry ||
-				    !S_ISREG(d_inode(filter->path.dentry)
-					     ->i_mode))
+				    !S_ISREG(d_inode(filter->path.dentry)->i_mode))
 					goto fail;
-
-				event->addr_filters.nr_file_filters++;
 			}
 
 			/* ready to consume more filters */
@@ -12343,9 +12391,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)
+perf_event_set_filter(struct perf_event *event, struct list_head *filters)
 {
-	LIST_HEAD(filters);
 	int ret;
 
 	/*
@@ -12357,65 +12404,17 @@ perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
 	if (WARN_ON_ONCE(event->parent))
 		return -EINVAL;
 
-	ret = perf_event_parse_addr_filter(event, filter_str, &filters);
+	ret = event->pmu->addr_filters_validate(filters);
 	if (ret)
-		goto fail_clear_files;
-
-	ret = event->pmu->addr_filters_validate(&filters);
-	if (ret)
-		goto fail_free_filters;
+		return ret;
 
 	/* remove existing filters, if any */
-	perf_addr_filters_splice(event, &filters);
+	perf_addr_filters_splice(event, filters);
 
 	/* install new filters */
 	perf_event_for_each_child(event, perf_event_addr_filters_apply);
 
-	return ret;
-
-fail_free_filters:
-	free_filters_list(&filters);
-
-fail_clear_files:
-	event->addr_filters.nr_file_filters = 0;
-
-	return ret;
-}
-
-static int perf_event_set_filter(struct perf_event *event, void __user *arg)
-{
-	int ret = -EINVAL;
-	char *filter_str;
-
-	filter_str = strndup_user(arg, PAGE_SIZE);
-	if (IS_ERR(filter_str))
-		return PTR_ERR(filter_str);
-
-#ifdef CONFIG_EVENT_TRACING
-	if (perf_event_is_tracing(event)) {
-		struct perf_event_context *ctx = event->ctx;
-
-		/*
-		 * Beware, here be dragons!!
-		 *
-		 * the tracepoint muck will deadlock against ctx->mutex, but
-		 * the tracepoint stuff does not actually need it. So
-		 * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we
-		 * already have a reference on ctx.
-		 *
-		 * This can result in event getting moved to a different ctx,
-		 * but that does not affect the tracepoint state.
-		 */
-		mutex_unlock(&ctx->mutex);
-		ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
-		mutex_lock(&ctx->mutex);
-	} else
-#endif
-	if (has_addr_filter(event))
-		ret = perf_event_set_addr_filter(event, filter_str);
-
-	kfree(filter_str);
-	return ret;
+	return 0;
 }
 
 /*

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/2] perf: look up address filter files without ctx->mutex held
  2026-11-27  9:18           ` Peter Zijlstra
@ 2026-11-27  9:45             ` Christian Brauner
  2026-11-27 11:57               ` Peter Zijlstra
  0 siblings, 1 reply; 12+ messages in thread
From: Christian Brauner @ 2026-11-27  9:45 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: linux-perf-users, linux-fsdevel, Ingo Molnar,
	Arnaldo Carvalho de Melo, Jann Horn, linux-kernel, stable,
	adrian.hunter, alexander.shishkin, dapeng1.mi

On Fri, Nov 27, 2026 at 10:18:52AM +0100, Peter Zijlstra wrote:
> On Fri, Nov 27, 2026 at 09:13:03AM +0100, Peter Zijlstra wrote:
> > On Thu, Nov 26, 2026 at 10:26:02PM +0100, Christian Brauner wrote:
> > > So ok, if we can't drop ctx->mutex in the middle of the ioctl then how
> > > about we don't hold ctx->mutex during parsing to begin with?
> > > We let perf_ioctl() hand PERF_EVENT_IOC_SET_FILTER off before it takes
> > > the mutex:
> > > 
> > > ioctl(PERF_EVENT_IOC_SET_FILTER)
> > > -> perf_ioctl()
> > >    -> perf_event_set_filter()                // no ctx->mutex
> > >       -> perf_event_set_addr_filter()
> > >          -> perf_event_parse_addr_filter()   // kern_path(), own list
> > >          -> perf_event_ctx_lock()
> > >             // revoked? has_addr_filter()? ->addr_filters_validate()
> > >             -> perf_addr_filters_splice()
> > >             -> perf_event_addr_filters_apply() on each child
> > >          -> perf_event_ctx_unlock()
> > > 
> > > The parser fills its own list and counts the file filters in locals.
> > > Everything that needs ctx->mutex then happens in one go under
> > > perf_event_ctx_lock().
> > > 
> > > So ctx->mutex is never dropped in the middle of an ioctl, a migrated
> > > event is handled by perf_event_ctx_lock() like everywhere else and no
> > > ctx is carried across the lookup?
> > 
> > Let me try this.
> 
> Compile tested only; Alexander, Adrian; could any of you take a hard
> look at this and actually test?
> 
> ---
>  kernel/events/core.c | 135 +++++++++++++++++++++++++--------------------------
>  1 file changed, 67 insertions(+), 68 deletions(-)
> 
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 77c501005143..c019e4faf655 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -6632,16 +6632,24 @@ static inline bool is_perf_file(struct fd f)
>  
>  static int perf_event_set_output(struct perf_event *event,
>  				 struct perf_event *output_event);
> -static int perf_event_set_filter(struct perf_event *event, void __user *arg);
> +static int perf_event_set_filter(struct perf_event *event, struct list_head *filters);
>  static int perf_copy_attr(struct perf_event_attr __user *uattr,
>  			  struct perf_event_attr *attr);
>  static int __perf_event_set_bpf_prog(struct perf_event *event,
>  				     struct bpf_prog *prog,
>  				     u64 bpf_cookie);
>  
> -static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
> +struct perf_ioctl_ctx {
> +	unsigned int cmd;
> +	unsigned long arg;
> +	struct list_head filters;
> +};
> +
> +static long _perf_ioctl(struct perf_event *event, struct perf_ioctl_ctx *ctx)
>  {
>  	void (*func)(struct perf_event *);
> +	unsigned long arg = ctx->arg;
> +	unsigned int cmd = ctx->cmd;
>  	u32 flags = arg;
>  
>  	if (event->state <= PERF_EVENT_STATE_REVOKED)
> @@ -6692,7 +6700,7 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
>  	}
>  
>  	case PERF_EVENT_IOC_SET_FILTER:
> -		return perf_event_set_filter(event, (void __user *)arg);
> +		return perf_event_set_filter(event, &ctx->filters);
>  
>  	case PERF_EVENT_IOC_SET_BPF:
>  	{
> @@ -6751,10 +6759,21 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
>  	return 0;
>  }
>  
> +static inline bool perf_event_is_tracing(struct perf_event *event);
> +static void free_filters_list(struct list_head *filters);
> +static int
> +perf_event_parse_addr_filter(int event_node, bool event_task, char *fstr,
> +			     struct list_head *filters);
> +
>  static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
>  {
>  	struct perf_event *event = file->private_data;
>  	struct perf_event_context *ctx;
> +	struct perf_ioctl_ctx ic = {
> +		.cmd = cmd,
> +		.arg = arg,
> +		.filters = LIST_HEAD_INIT(ic.filters),
> +	};
>  	long ret;
>  
>  	/* Treat ioctl like writes as it is likely a mutating operation. */
> @@ -6762,10 +6781,34 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
>  	if (ret)
>  		return ret;
>  
> +	if (cmd == PERF_EVENT_IOC_SET_FILTER) {
> +		int cpu = READ_ONCE(event->cpu);
> +		int node = cpu == -1 ? NUMA_NO_NODE : cpu_to_node(cpu);
> +		bool task = event->attach_state & PERF_ATTACH_TASK;
> +		char *filter_str __free(kfree) =
> +			strndup_user((void __user *)arg, PAGE_SIZE);
> +
> +		if (IS_ERR(filter_str))
> +			return PTR_ERR(filter_str);
> +
> +#ifdef CONFIG_EVENT_TRACING
> +		if (perf_event_is_tracing(event)) {
> +			return ftrace_profile_set_filter(event, event->attr.config,
> +							filter_str);
> +		}
> +#endif
> +		ret = perf_event_parse_addr_filter(node, task, filter_str,
> +						   &ic.filters);
> +		if (ret)
> +			return ret;
> +	}
> +
>  	ctx = perf_event_ctx_lock(event);
> -	ret = _perf_ioctl(event, cmd, arg);
> +	ret = _perf_ioctl(event, &ic);
>  	perf_event_ctx_unlock(event, ctx);
>  
> +	free_filters_list(&ic.filters);
> +
>  	return ret;
>  }
>  
> @@ -11990,9 +12033,8 @@ void perf_bp_event(struct perf_event *bp, void *data)
>   * Allocate a new address filter
>   */
>  static struct perf_addr_filter *
> -perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
> +perf_addr_filter_new(int node, struct list_head *filters)
>  {
> -	int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
>  	struct perf_addr_filter *filter;
>  
>  	filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
> @@ -12035,8 +12077,17 @@ static void perf_addr_filters_splice(struct perf_event *event,
>  	raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
>  
>  	list_splice_init(&event->addr_filters.list, &list);
> -	if (head)
> +	event->addr_filters.nr_file_filters = 0;
> +
> +	if (head) {
> +		struct perf_addr_filter *filter;
> +
>  		list_splice(head, &event->addr_filters.list);
> +		list_for_each_entry(filter, &event->addr_filters.list, entry) {
> +			if (filter->path.dentry)
> +				event->addr_filters.nr_file_filters++;
> +		}
> +	}
>  
>  	raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
>  
> @@ -12186,7 +12237,7 @@ static const match_table_t if_tokens = {
>   * Address filter string parser
>   */
>  static int
> -perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
> +perf_event_parse_addr_filter(int event_node, bool event_task, char *fstr,
>  			     struct list_head *filters)
>  {
>  	struct perf_addr_filter *filter = NULL;
> @@ -12213,7 +12264,7 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
>  
>  		/* filter definition begins */
>  		if (state == IF_STATE_ACTION) {
> -			filter = perf_addr_filter_new(event, filters);
> +			filter = perf_addr_filter_new(event_node, filters);
>  			if (!filter)
>  				goto fail;
>  		}
> @@ -12299,7 +12350,7 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
>  				 * different processes.
>  				 */
>  				ret = -EOPNOTSUPP;
> -				if (!event->ctx->task)
> +				if (!event_task)
>  					goto fail;
>  
>  				/* look up the path and grab its inode */
> @@ -12310,11 +12361,8 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
>  
>  				ret = -EINVAL;
>  				if (!filter->path.dentry ||
> -				    !S_ISREG(d_inode(filter->path.dentry)
> -					     ->i_mode))
> +				    !S_ISREG(d_inode(filter->path.dentry)->i_mode))
>  					goto fail;

Just a thought on this. In vfs-7.5.lookup I have:

file_check_backing() which is the new default way to figure out whether
something can be a backing file for a given subsystem. Its checks are:

int file_check_backing(const struct file *file, fmode_t mode, umode_t type)
{
	fmode_t io = mode & (FMODE_READ | FMODE_WRITE);
	/* Drivers may point ->f_mapping at something else. */
	umode_t fmt = file_inode(file)->i_mode & S_IFMT;
	struct inode *real_inode;

	/* O_PATH files are only fine for callers that just use the path. */
	if ((file->f_mode & FMODE_PATH) && !(mode & FMODE_PATH))
		return -EBADF;

	if ((file->f_mode & io) != io)
		return -EBADF;

	if (fmt != S_IFREG && !(type == S_IFBLK && fmt == S_IFBLK))
		return -EINVAL;

	/* Look through stacking filesystems at the inode with the data. */
	real_inode = d_real_data_inode(file->f_path.dentry);
	if (IS_ERR(real_inode))
		return PTR_ERR(real_inode);
	if (real_inode->i_sb->s_iflags & SB_I_PSEUDO)
		return -EINVAL;

	if ((mode & FMODE_READ) &&
	    (!(file->f_mode & FMODE_CAN_READ) || !file->f_op->read_iter))
		return -EINVAL;

	if ((mode & FMODE_WRITE) &&
	    (!(file->f_mode & FMODE_CAN_WRITE) || !file->f_op->write_iter))
		return -EINVAL;

	return 0;
}

It excludes anything that is a pseudo filesystem:

arch/powerpc/platforms/cell/spufs/inode.c:      fc_init_pseudo(fc);
arch/s390/hypfs/inode.c:        fc_init_pseudo(fc);
drivers/android/binder/rust_binderfs.c: fc_init_pseudo(fc);
drivers/usb/gadget/function/f_fs.c:     fc_init_pseudo(fc);
drivers/usb/gadget/legacy/inode.c:      fc_init_pseudo(fc);
drivers/xen/xenfs/super.c:      fc_init_pseudo(fc);
fs/binfmt_misc.c:       fc_init_pseudo(fc);
fs/configfs/mount.c:    fc_init_pseudo(fc);
fs/debugfs/inode.c:     fc_init_pseudo(fc);
fs/devpts/inode.c:      fc_init_pseudo(fc);
fs/efivarfs/super.c:    fc_init_pseudo(fc);
fs/failfs.c:    fc_init_pseudo(fc);
fs/fuse/control.c:      fc_init_pseudo(fsc);
fs/kernfs/mount.c:      fc_init_pseudo(fc);
fs/libfs.c:             fc_init_pseudo(fc);
fs/nfsd/nfsctl.c:       fc_init_pseudo(fc);
fs/nullfs.c:    fc_init_pseudo(fc);
fs/ocfs2/dlmfs/dlmfs.c: fc_init_pseudo(fc);
fs/openpromfs/inode.c:  fc_init_pseudo(fc);
fs/proc/root.c: fc_init_pseudo(fc);
fs/pstore/inode.c:      fc_init_pseudo(fc);
fs/tracefs/inode.c:     fc_init_pseudo(fc);
include/linux/pseudo_fs.h:static inline void fc_init_pseudo(struct fs_context *fc)
ipc/mqueue.c:   fc_init_pseudo(fc);
kernel/bpf/inode.c:     fc_init_pseudo(fc);
net/sunrpc/rpc_pipe.c:  fc_init_pseudo(fc);
security/apparmor/apparmorfs.c: fc_init_pseudo(fc);
security/inode.c:       fc_init_pseudo(fc);
security/selinux/selinuxfs.c:   fc_init_pseudo(fc);
security/smack/smackfs.c:       fc_init_pseudo(fc);

I don't think anything on this list is something that you would ever
want to use as an object file?

I think we should do an O_PATH open of the target file and then you
call:

file = filp_open(filename, O_PATH | O_REGULAR, 0)

if (file_check_backing(file, FMODE_PATH, 0))
	return -EINVAL;

We had a whole bug class where various subsystems that used backing
files just allowed arbitrary files to be used.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/2] perf: look up address filter files without ctx->mutex held
  2026-11-27  9:45             ` Christian Brauner
@ 2026-11-27 11:57               ` Peter Zijlstra
  0 siblings, 0 replies; 12+ messages in thread
From: Peter Zijlstra @ 2026-11-27 11:57 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-perf-users, linux-fsdevel, Ingo Molnar,
	Arnaldo Carvalho de Melo, Jann Horn, linux-kernel, stable,
	adrian.hunter, alexander.shishkin, dapeng1.mi

On Fri, Nov 27, 2026 at 10:45:28AM +0100, Christian Brauner wrote:

> > @@ -12310,11 +12361,8 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
> >  
> >  				ret = -EINVAL;
> >  				if (!filter->path.dentry ||
> > -				    !S_ISREG(d_inode(filter->path.dentry)
> > -					     ->i_mode))
> > +				    !S_ISREG(d_inode(filter->path.dentry)->i_mode))
> >  					goto fail;
> 
> Just a thought on this. In vfs-7.5.lookup I have:
> 
> file_check_backing() which is the new default way to figure out whether
> something can be a backing file for a given subsystem. Its checks are:
> 
> int file_check_backing(const struct file *file, fmode_t mode, umode_t type)
> {
> 	fmode_t io = mode & (FMODE_READ | FMODE_WRITE);
> 	/* Drivers may point ->f_mapping at something else. */
> 	umode_t fmt = file_inode(file)->i_mode & S_IFMT;
> 	struct inode *real_inode;
> 
> 	/* O_PATH files are only fine for callers that just use the path. */
> 	if ((file->f_mode & FMODE_PATH) && !(mode & FMODE_PATH))
> 		return -EBADF;
> 
> 	if ((file->f_mode & io) != io)
> 		return -EBADF;
> 
> 	if (fmt != S_IFREG && !(type == S_IFBLK && fmt == S_IFBLK))
> 		return -EINVAL;
> 
> 	/* Look through stacking filesystems at the inode with the data. */
> 	real_inode = d_real_data_inode(file->f_path.dentry);
> 	if (IS_ERR(real_inode))
> 		return PTR_ERR(real_inode);
> 	if (real_inode->i_sb->s_iflags & SB_I_PSEUDO)
> 		return -EINVAL;
> 
> 	if ((mode & FMODE_READ) &&
> 	    (!(file->f_mode & FMODE_CAN_READ) || !file->f_op->read_iter))
> 		return -EINVAL;
> 
> 	if ((mode & FMODE_WRITE) &&
> 	    (!(file->f_mode & FMODE_CAN_WRITE) || !file->f_op->write_iter))
> 		return -EINVAL;
> 
> 	return 0;
> }
> 
> It excludes anything that is a pseudo filesystem:
> 
> arch/powerpc/platforms/cell/spufs/inode.c:      fc_init_pseudo(fc);
> arch/s390/hypfs/inode.c:        fc_init_pseudo(fc);
> drivers/android/binder/rust_binderfs.c: fc_init_pseudo(fc);
> drivers/usb/gadget/function/f_fs.c:     fc_init_pseudo(fc);
> drivers/usb/gadget/legacy/inode.c:      fc_init_pseudo(fc);
> drivers/xen/xenfs/super.c:      fc_init_pseudo(fc);
> fs/binfmt_misc.c:       fc_init_pseudo(fc);
> fs/configfs/mount.c:    fc_init_pseudo(fc);
> fs/debugfs/inode.c:     fc_init_pseudo(fc);
> fs/devpts/inode.c:      fc_init_pseudo(fc);
> fs/efivarfs/super.c:    fc_init_pseudo(fc);
> fs/failfs.c:    fc_init_pseudo(fc);
> fs/fuse/control.c:      fc_init_pseudo(fsc);
> fs/kernfs/mount.c:      fc_init_pseudo(fc);
> fs/libfs.c:             fc_init_pseudo(fc);
> fs/nfsd/nfsctl.c:       fc_init_pseudo(fc);
> fs/nullfs.c:    fc_init_pseudo(fc);
> fs/ocfs2/dlmfs/dlmfs.c: fc_init_pseudo(fc);
> fs/openpromfs/inode.c:  fc_init_pseudo(fc);
> fs/proc/root.c: fc_init_pseudo(fc);
> fs/pstore/inode.c:      fc_init_pseudo(fc);
> fs/tracefs/inode.c:     fc_init_pseudo(fc);
> include/linux/pseudo_fs.h:static inline void fc_init_pseudo(struct fs_context *fc)
> ipc/mqueue.c:   fc_init_pseudo(fc);
> kernel/bpf/inode.c:     fc_init_pseudo(fc);
> net/sunrpc/rpc_pipe.c:  fc_init_pseudo(fc);
> security/apparmor/apparmorfs.c: fc_init_pseudo(fc);
> security/inode.c:       fc_init_pseudo(fc);
> security/selinux/selinuxfs.c:   fc_init_pseudo(fc);
> security/smack/smackfs.c:       fc_init_pseudo(fc);
> 
> I don't think anything on this list is something that you would ever
> want to use as an object file?

Very very possibly people would like /proc/kcore to self reference or
something, but I'm not sure that's a thing. Adrian?

> I think we should do an O_PATH open of the target file and then you
> call:
> 
> file = filp_open(filename, O_PATH | O_REGULAR, 0)
> 
> if (file_check_backing(file, FMODE_PATH, 0))
> 	return -EINVAL;

We must close file, or does it have __free(file) on or something?

> We had a whole bug class where various subsystems that used backing
> files just allowed arbitrary files to be used.

This seems fine to me and can go in independent of this locking stuff.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-11-27 11:57 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 11:45 [PATCH 0/2] exec: two more sleepers in the way of exec Christian Brauner
2026-11-26 11:45 ` [PATCH 1/2] perf: look up address filter files without ctx->mutex held Christian Brauner
2026-11-26 15:24   ` Peter Zijlstra
2026-11-26 20:56     ` Christian Brauner
2026-11-26 21:26       ` Christian Brauner
2026-11-26 21:59         ` Christian Brauner
2026-11-27  8:13         ` Peter Zijlstra
2026-11-27  9:18           ` Peter Zijlstra
2026-11-27  9:45             ` Christian Brauner
2026-11-27 11:57               ` Peter Zijlstra
2026-11-26 11:45 ` [PATCH 2/2] proc: call task_vsize() without exec_update_lock held Christian Brauner
2026-11-26 11:54 ` [PATCH 0/2] exec: two more sleepers in the way of exec sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox