]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
fs: Use try_cmpxchg() in start_dir_add()
authorUros Bizjak <ubizjak@gmail.com>
Mon, 11 Aug 2025 12:52:38 +0000 (14:52 +0200)
committerChristian Brauner <brauner@kernel.org>
Mon, 25 Aug 2025 12:09:00 +0000 (14:09 +0200)
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>
fs/dcache.c

index 60046ae23d5148730888da2250bd72df317d65ad..336bdb4c4b1f4e805cd3b508cca9b65c31bcb36c 100644 (file)
@@ -2509,8 +2509,8 @@ static inline unsigned start_dir_add(struct inode *dir)
 {
        preempt_disable_nested();
        for (;;) {
-               unsigned n = dir->i_dir_seq;
-               if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n)
+               unsigned n = READ_ONCE(dir->i_dir_seq);
+               if (!(n & 1) && try_cmpxchg(&dir->i_dir_seq, &n, n + 1))
                        return n;
                cpu_relax();
        }