From: Uros Bizjak Date: Thu, 23 May 2024 07:35:14 +0000 (+0200) Subject: fork: use this_cpu_try_cmpxchg() in try_release_thread_stack_to_cache() X-Git-Tag: v6.11-rc1~84^2~94 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=47e39c79336760031f98215c014c9800f5ed6481;p=thirdparty%2Fkernel%2Flinux.git fork: use this_cpu_try_cmpxchg() in try_release_thread_stack_to_cache() Use this_cpu_try_cmpxchg() instead of this_cpu_cmpxchg (*ptr, old, new) == old in try_release_thread_stack_to_cache. x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg). No functional change intended. [ubizjak@gmail.com: simplify the for loop a bit] Link: https://lkml.kernel.org/r/20240523214442.21102-1-ubizjak@gmail.com Link: https://lkml.kernel.org/r/20240523073530.8128-1-ubizjak@gmail.com Signed-off-by: Uros Bizjak Cc: Oleg Nesterov Cc: Kees Cook Signed-off-by: Andrew Morton --- diff --git a/kernel/fork.c b/kernel/fork.c index 99076dbe27d83..f1c16b3dd8ac6 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -205,9 +205,10 @@ static bool try_release_thread_stack_to_cache(struct vm_struct *vm) unsigned int i; for (i = 0; i < NR_CACHED_STACKS; i++) { - if (this_cpu_cmpxchg(cached_stacks[i], NULL, vm) != NULL) - continue; - return true; + struct vm_struct *tmp = NULL; + + if (this_cpu_try_cmpxchg(cached_stacks[i], &tmp, vm)) + return true; } return false; }