Skip to contentSkip to conversation

futex: Use atomic64_inc_return() in get_inode_sequence_number()

PATCH Applied Uros Bizjak 3 participants 6 messages Oct 10, 2024 – Oct 17, 2024 Futex

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 futex: Use atomic64_inc_return() in get_inode_sequence_number()

Use atomic64_inc_return(&ref) instead of atomic64_add_return(1, &ref) to use optimized implementation and ease register pressure around the primitive for targets that implement optimized variant.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Darren Hart <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc "André Almeida" <[email protected]>
---
 kernel/futex/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/futex/core.c +1 -1
index 136768ae2637..3146730e55f7 100644
@@ -181,7 +181,7 @@ static u64 get_inode_sequence_number(struct inode *inode)
 		return old;
 
 	for (;;) {
-		u64 new = atomic64_add_return(1, &i_seq);
+		u64 new = atomic64_inc_return(&i_seq);
 		if (WARN_ON_ONCE(!new))
 			continue;
 
Signature · 1 line
2.46.2
#1 lore raw

PATCH 2/2 futex: Use atomic64_try_cmpxchg_relaxed() in get_inode_sequence_number()

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

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Darren Hart <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc "André Almeida" <[email protected]>
---
 kernel/futex/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/futex/core.c +3 -2
index 3146730e55f7..11795439efb7 100644
@@ -185,8 +185,9 @@ 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;
 	}
Signature · 1 line
2.46.2
reviewer Reviewed-by on v1 1/2
#4 lore raw
In reply to Uros Bizjak

Em 10/10/2024 04:10, Uros Bizjak escreveu:

Quoted 4 lines

Use atomic64_inc_return(&ref) instead of atomic64_add_return(1, &ref) to use optimized implementation and ease register pressure around the primitive for targets that implement optimized variant.

Quoted 8 lines
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Darren Hart <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc "André Almeida" <[email protected]>

---

reviewer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] futex: Use atomic64_try_cmpxchg_relaxed() in get_inode_sequence_number()

In reply to Uros Bizjak

Em 10/10/2024 04:10, Uros Bizjak escreveu:

Quoted 25 lines

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

Quoted 7 lines
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Darren Hart <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc "André Almeida" <[email protected]>
merge bot Applied
#3 lore raw

[tip: locking/core] futex: Use atomic64_try_cmpxchg_relaxed() in get_inode_sequence_number()

In reply to Uros Bizjak
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     87347f148061b48c3495fb61dcbad384760da9cf
Gitweb:        https://git.kernel.org/tip/87347f148061b48c3495fb61dcbad384760da9cf
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Thu, 10 Oct 2024 09:10:05 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Thu, 17 Oct 2024 22:02:27 +02:00

futex: Use atomic64_try_cmpxchg_relaxed() in get_inode_sequence_number()

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

---
 kernel/futex/core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/futex/core.c +2 -2
index 3146730..692912b 100644
@@ -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;
 	}
merge bot Applied
#5 lore raw
In reply to Uros Bizjak
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     19298f48694987fac843261c84e24834c255b451
Gitweb:        https://git.kernel.org/tip/19298f48694987fac843261c84e24834c255b451
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Thu, 10 Oct 2024 09:10:04 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Thu, 17 Oct 2024 22:02:27 +02:00

futex: Use atomic64_inc_return() in get_inode_sequence_number()

Use atomic64_inc_return(&ref) instead of atomic64_add_return(1, &ref) to use optimized implementation and ease register pressure around the primitive for targets that implement optimized variant.

---
 kernel/futex/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/futex/core.c +1 -1
index 136768a..3146730 100644
@@ -181,7 +181,7 @@ static u64 get_inode_sequence_number(struct inode *inode)
 		return old;
 
 	for (;;) {
-		u64 new = atomic64_add_return(1, &i_seq);
+		u64 new = atomic64_inc_return(&i_seq);
 		if (WARN_ON_ONCE(!new))
 			continue;