Re: [PATCH 1/2] perf: look up address filter files without ctx->mutex held

From: Christian Brauner

Date: Thu Nov 26 2026 - 16:26:18 EST


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?