]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
futex: Use atomic64_try_cmpxchg_relaxed() in get_inode_sequence_number()
authorUros Bizjak <ubizjak@gmail.com>
Thu, 10 Oct 2024 07:10:05 +0000 (09:10 +0200)
committerThomas Gleixner <tglx@linutronix.de>
Thu, 17 Oct 2024 20:02:27 +0000 (22:02 +0200)
Optimize get_inode_sequence_number() to use simpler and faster:

  !atomic64_try_cmpxchg_relaxed(*ptr, &old, new)

instead of:

  atomic64_cmpxchg relaxed(*ptr, old, new) != old

The x86 CMPXCHG instruction returns success in ZF flag, so
this change saves a compare after cmpxchg. The generated
code improves from:

 3da: 31 c0                 xor    %eax,%eax
 3dc: f0 48 0f b1 8a 38 01  lock cmpxchg %rcx,0x138(%rdx)
 3e3: 00 00
 3e5: 48 85 c0              test   %rax,%rax
 3e8: 48 0f 44 c1           cmove  %rcx,%rax

to:

 3da: 31 c0                 xor    %eax,%eax
 3dc: f0 48 0f b1 8a 38 01  lock cmpxchg %rcx,0x138(%rdx)
 3e3: 00 00
 3e5: 48 0f 44 c1           cmove  %rcx,%rax

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: André Almeida <andrealmeid@igalia.com>
Link: https://lore.kernel.org/all/20241010071023.21913-2-ubizjak@gmail.com
kernel/futex/core.c

index 3146730e55f75a7d6474209813e09592b7472517..692912bf1252c43a115c39489fb0ad1af748f54c 100644 (file)
@@ -185,8 +185,8 @@ static u64 get_inode_sequence_number(struct inode *inode)
                if (WARN_ON_ONCE(!new))
                        continue;
 
-               old = atomic64_cmpxchg_relaxed(&inode->i_sequence, 0, new);
-               if (old)
+               old = 0;
+               if (!atomic64_try_cmpxchg_relaxed(&inode->i_sequence, &old, new))
                        return old;
                return new;
        }