Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fs: Use try_cmpxchg() in start_dir_add()

Use try_cmpxchg() instead of cmpxchg(*ptr, old, new) == old.

The x86 CMPXCHG instruction returns success in the ZF flag,
so this change saves a compare after CMPXCHG (and related
move instruction in front of CMPXCHG).

Note that the value from *ptr should be read using READ_ONCE() to
prevent the compiler from merging, refetching or reordering the read.

No functional change intended.

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Link: https://lore.kernel.org/20250811125308.616717-1-ubizjak@gmail.com
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Christian Brauner <brauner@kernel.org>

authored by

Uros Bizjak and committed by
Christian Brauner
14498ca7 278033a2

+2 -2
+2 -2
fs/dcache.c
··· 2509 2509 { 2510 2510 preempt_disable_nested(); 2511 2511 for (;;) { 2512 - unsigned n = dir->i_dir_seq; 2513 - if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n) 2512 + unsigned n = READ_ONCE(dir->i_dir_seq); 2513 + if (!(n & 1) && try_cmpxchg(&dir->i_dir_seq, &n, n + 1)) 2514 2514 return n; 2515 2515 cpu_relax(); 2516 2516 }