Skip to content

Commit 8f19331

Browse files
mjguzikkees
authored andcommitted
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. Signed-off-by: Mateusz Guzik <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent e1cec51 commit 8f19331

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

kernel/seccomp.c

+10
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ void seccomp_filter_release(struct task_struct *tsk)
574574
if (WARN_ON((tsk->flags & PF_EXITING) == 0))
575575
return;
576576

577+
if (READ_ONCE(tsk->seccomp.filter) == NULL)
578+
return;
579+
577580
spin_lock_irq(&tsk->sighand->siglock);
578581
orig = tsk->seccomp.filter;
579582
/* Detach task from its filter tree. */
@@ -599,6 +602,13 @@ static inline void seccomp_sync_threads(unsigned long flags)
599602
BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
600603
assert_spin_locked(&current->sighand->siglock);
601604

605+
/*
606+
* Don't touch any of the threads if the process is being killed.
607+
* This allows for a lockless check in seccomp_filter_release.
608+
*/
609+
if (current->signal->flags & SIGNAL_GROUP_EXIT)
610+
return;
611+
602612
/* Synchronize all threads. */
603613
caller = current;
604614
for_each_thread(caller, thread) {

0 commit comments

Comments
 (0)