seccomp: avoid the lock trip seccomp_filter_release in common case
PATCH v2 1/1 seccomp: avoid the lock trip seccomp_filter_release in common case
Vast majority of threads don't have any seccomp filters, all while the lock taken here is shared between all threads in given process and frequently used.
Safety of the check relies on the following:
- seccomp_filter_release is only legally called for PF_EXITING threads
- SIGNAL_GROUP_EXIT is only ever set with the sighand lock held
- PF_EXITING is only ever set with the sighand lock held *or* after SIGNAL_GROUP_EXIT is set *or* the process is single-threaded
- seccomp_sync_threads holds the sighand lock and skips all threads if SIGNAL_GROUP_EXIT is set, PF_EXITING threads if not
Resulting reduction of contention gives me a 5% boost in a microbenchmark spawning and killing threads within the same process.
---
So I slept on it and did an actual analysis instead of a lazy skim, benchmarked as well. :)
kernel/seccomp.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
kernel/seccomp.c +10 -0
@@ -574,6 +574,9 @@ void seccomp_filter_release(struct task_struct *tsk)if (WARN_ON ((tsk->flags &PF_EXITING ) ==0 ))return ; +if (READ_ONCE (tsk->seccomp.filter) ==NULL ) +return ; +spin_lock_irq (&tsk->sighand->siglock); orig = tsk->seccomp.filter;/* Detach task from its filter tree. */
@@ -599,6 +602,13 @@ static inline void seccomp_sync_threads(unsigned long flags)BUG_ON (!mutex_is_locked (¤t->signal->cred_guard_mutex));assert_spin_locked (¤t->sighand->siglock); +/* +* Don't touch any of the threads if the process is being killed. +* This allows for a lockless check in seccomp_filter_release. +*/ +if (current->signal->flags &SIGNAL_GROUP_EXIT ) +return ; +/* Synchronize all threads. */ caller = current;for_each_thread (caller, thread) {
Signature · 1 line
2.43.0
Quoted 12 lines · Mateusz Guzik
Vast majority of threads don't have any seccomp filters, all while the lock taken here is shared between all threads in given process and frequently used.
Safety of the check relies on the following:
-seccomp_filter_releaseis only legally called forPF_EXITINGthreads
-SIGNAL_GROUP_EXITis only ever set with the sighand lock held
-PF_EXITINGis only ever set with the sighand lock held *or* afterSIGNAL_GROUP_EXITis set *or* the process is single-threaded
-seccomp_sync_threadsholds the sighand lock and skips all threads ifSIGNAL_GROUP_EXITis set,PF_EXITINGthreads if not
Quoted 1 line
[...]
Applied to for-next/seccomp, thanks!
[1/1] seccomp: avoid the lock trip seccomp_filter_release in common case
https://git.kernel.org/kees/c/8f19331384e6
Take care,
Signature · 1 line
Kees Cook