Skip to content

RFC exec: move sleeping work out of exec_update_lock

Christian Brauner bpf linux-fsdevel linux-mm linux-perf-users linux-security-module selinux stable 1 version New

Get series with b4
b4 am [email protected] change-id 20261120-work-exec-update_lock-7f4bec04bd81

Versions newest first

  1. v1 RFC Latest19 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/19proc: copy namespace link after dropping exec_update_lockfs/proc/namespaces.c+3 -40 replies
2/19kcmp: look up epoll target without exec_update_lock heldkernel/kcmp.c+50 -220 replies
3/19perf: copy counter values after dropping ctx->mutexkernel/events/core.c+25 -180 replies
4/19perf: copy ioctl arguments without ctx->mutex heldkernel/events/core.c+57 -360 replies
5/19selinux: close inherited files that can't be replacedsecurity/selinux/hooks.c+3 -10 replies
6/19exec: move unshare_sighand() before exec_mmap()fs/exec.c+8 -70 replies
7/19selftests/exec: test exec with a task sleeping in a page faulttools/testing/selftests/exec/.gitignore tools/testing/selftests/exec/Makefile tools/testing/selftests/exec/config +1 more+554 -00 replies
8/19selftests/exec: test exec with a shared signal tabletools/testing/selftests/exec/.gitignore tools/testing/selftests/exec/Makefile tools/testing/selftests/exec/unshare-sighand.c+239 -00 replies
9/19lsm: add security_bprm_revalidate_files()fs/exec.c include/linux/lsm_hook_defs.h include/linux/security.h +1 more+33 -30 replies
10/19bpf: mark bprm_revalidate_files as sleepablekernel/bpf/bpf_lsm.c+1 -00 replies
11/19selinux: revalidate inherited files before taking exec_update_locksecurity/selinux/hooks.c+15 -30 replies
12/19apparmor: revalidate inherited files before taking exec_update_locksecurity/apparmor/lsm.c+21 -20 replies
13/19exec: drop exec_update_lock after the credentials are committedfs/exec.c kernel/events/core.c+25 -160 replies
14/19exec: switch time namespace after dropping exec_update_lockfs/exec.c+5 -40 replies
15/19exec: don't take the mmap lock of the old mmfs/exec.c+0 -140 replies
16/19exit: don't take the mmap lock in exit_mm()kernel/exit.c+0 -30 replies
17/19proc: look up map_files vma without exec_update_lock heldfs/proc/base.c+52 -160 replies
18/19vfork: wake up the parent once the child has left the mmfs/exec.c include/linux/sched/mm.h kernel/exit.c +1 more+25 -130 replies
19/19selftests/exec: test when a vfork() parent is woken uptools/testing/selftests/exec/.gitignore tools/testing/selftests/exec/Makefile tools/testing/selftests/exec/vfork-mm.c+365 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock

I've been massaging exec quite a bit to move a bunch of stuff around and get rid of a lot of sleeping work out of that path or at least move it out from precious locks that are used for access checks.

This is another round I plan to upstream. The gist is that we're relying on exec_update_lock in a bunch more places now and we've been willy-nilly moving stuff beneath that lock for a while. That's not sustainable. We need to clean that up and really only do the absolute minimum of work under that lock to avoid stalling tasks indefinitely just because stalls an exec. It's also just good hygiene because it's getting really hard to understand what that lock really protects.

I've spent some time playing with various patches. Some of them went out independently. This is another core chunk:

* proc_ns_readlink() copies the link to userspace with the lock held.

* kcmp(KCMP_EPOLL_TFD) copies the epoll slot from userspace with the lock of both tasks held and then waits for the epoll mutex which epoll_wait() holds across a page fault.

* perf_read() and perf_ioctl() copy to and from userspace with the mutex of the perf context held. perf_event_exec() takes that mutex with exec_update_lock held.

* SELinux ignores a failing replace_fd() when it revokes inherited files during exec.

* A vfork() parent is woken up in exec_mmap() but a child that shares its signal table with the parent only gets a private one after that.

* SELinux and AppArmor revalidate the inherited files from the bprm_committing_creds hook, i.e., with the lock held.

* Move the time namespace switch out of the lock. It doesn't need it.

* Drop the lock right after the new credentials are committed instead of in setup_new_exec().

* Don't take the mmap lock of the old mm in exec_mmap().

* Look up the vma for a link in /proc/<pid>/map_files without exec_update_lock held.

While at it, don't take the mmap lock in exit_mm() either. It's the same leftover. A vfork() child that exits without having exec'ed waits on the mmap lock of the parent it has just woken up.

* A vfork() parent is woken up before the child has left the mm, in exec as well as in exit.

What's left under the lock that can block is the hardware breakpoint flush in flush_thread(), perf_event_exec(), perf_event_exit_task(), the allocation in set_cred_ucounts() and a sleepable bpf lsm program on the bprm_committing_creds hook.

The perf ones still take the mutex of the perf context but perf_read() and perf_ioctl() don't sleep in a page fault with it held anymore.

---
Christian Brauner (19):
proc: copy namespace link after dropping exec_update_lock
kcmp: look up epoll target without exec_update_lock held
perf: copy counter values after dropping ctx->mutex
perf: copy ioctl arguments without ctx->mutex held
selinux: close inherited files that can't be replaced
exec: move unshare_sighand() before exec_mmap() selftests/exec: test exec with a task sleeping in a page fault selftests/exec: test exec with a shared signal table
lsm: add security_bprm_revalidate_files()
bpf: mark bprm_revalidate_files as sleepable
selinux: revalidate inherited files before taking exec_update_lock
apparmor: revalidate inherited files before taking exec_update_lock
exec: drop exec_update_lock after the credentials are committed
exec: switch time namespace after dropping exec_update_lock
exec: don't take the mmap lock of the old mm
exit: don't take the mmap lock in exit_mm()
proc: look up map_files vma without exec_update_lock held
vfork: wake up the parent once the child has left the mm selftests/exec: test when a vfork() parent is woken up

 fs/exec.c                                        |  81 ++--
 fs/proc/base.c                                   |  68 ++-
 fs/proc/namespaces.c                             |   7 +-
 include/linux/lsm_hook_defs.h                    |   1 +
 include/linux/sched/mm.h                         |   4 +-
 include/linux/security.h                         |   5 +
 kernel/bpf/bpf_lsm.c                             |   1 +
 kernel/events/core.c                             | 138 +++---
 kernel/exit.c                                    |  10 +-
 kernel/fork.c                                    |  24 +-
 kernel/kcmp.c                                    |  72 ++-
 security/apparmor/lsm.c                          |  23 +-
 security/security.c                              |  27 +-
 security/selinux/hooks.c                         |  22 +-
 tools/testing/selftests/exec/.gitignore          |   3 +
 tools/testing/selftests/exec/Makefile            |   6 +
 tools/testing/selftests/exec/config              |   3 +
 tools/testing/selftests/exec/unshare-sighand.c   | 236 ++++++++++
 tools/testing/selftests/exec/update-lock-fault.c | 547 +++++++++++++++++++++++
 tools/testing/selftests/exec/vfork-mm.c          | 363 +++++++++++++++
 20 files changed, 1480 insertions(+), 161 deletions(-)

---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261120-work-exec-update_lock-7f4bec04bd81