Skip to contentSkip to conversation

sched/rt: Use atomic_try_cmpxchg_acquire() in rto_start_trylock()

PATCH In review David Carlier 2 participants 2 messages Apr 15 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/rt: Use atomic_try_cmpxchg_acquire() in rto_start_trylock()

Replace this pattern in rto_start_trylock():

!atomic_cmpxchg_acquire(*ptr, 0, new)

... with the simpler and faster:

atomic_try_cmpxchg_acquire(*ptr, &zero, new)

The x86 CMPXCHG instruction returns success in the ZF flag, so atomic_try_cmpxchg_acquire() saves a compare after the CMPXCHG.

No functional change intended.

---
 kernel/sched/rt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/rt.c +2 -1
index 4ee8faf01441..13ac94899c18 100644
@@ -2172,7 +2172,8 @@ static int rto_next_cpu(struct root_domain *rd)
 
 static inline bool rto_start_trylock(atomic_t *v)
 {
-	return !atomic_cmpxchg_acquire(v, 0, 1);
+	int zero = 0;
+	return atomic_try_cmpxchg_acquire(v, &zero, 1);
 }
 
 static inline void rto_start_unlock(atomic_t *v)
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · David Carlier

Replace this pattern in rto_start_trylock():

!atomic_cmpxchg_acquire(*ptr, 0, new)

... with the simpler and faster:

Quoted 8 lines

atomic_try_cmpxchg_acquire(*ptr, &zero, new)

The x86 CMPXCHG instruction returns success in the ZF flag, so atomic_try_cmpxchg_acquire() saves a compare after the CMPXCHG.

No functional change intended.

Quoted 18 lines
---
  kernel/sched/rt.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/rt.c +2 -1
index 4ee8faf01441..13ac94899c18 100644
@@ -2172,7 +2172,8 @@ static int rto_next_cpu(struct root_domain *rd)
 
  static inline bool rto_start_trylock(atomic_t *v)
  {
-	return !atomic_cmpxchg_acquire(v, 0, 1);
+	int zero = 0;
+	return atomic_try_cmpxchg_acquire(v, &zero, 1);
  }
 
  static inline void rto_start_unlock(atomic_t *v)