Skip to contentSkip to conversation

kexec: Use atomic_try_cmpxchg_acquire() in kexec_trylock()

PATCH Applied Uros Bizjak 2 participants 4 messages Jul 19, 2024 – Jul 22, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kexec: Use atomic_try_cmpxchg_acquire() in kexec_trylock()

Use atomic_try_cmpxchg_acquire(*ptr, &old, new) instead of atomic_cmpxchg_acquire(*ptr, old, new) == old in kexec_trylock(). x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg.

Cc Eric Biederman <[email protected]>
---
 kernel/kexec_internal.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/kexec_internal.h +2 -1
index 2595defe8c0d..d35d9792402d 100644
@@ -23,7 +23,8 @@ int kimage_is_destination_range(struct kimage *image,
 extern atomic_t __kexec_lock;
 static inline bool kexec_trylock(void)
 {
-	return atomic_cmpxchg_acquire(&__kexec_lock, 0, 1) == 0;
+	int old = 0;
+	return atomic_try_cmpxchg_acquire(&__kexec_lock, &old, 1);
 }
 static inline void kexec_unlock(void)
 {
Signature · 1 line
2.42.0
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Uros Bizjak

Use atomic_try_cmpxchg_acquire(*ptr, &old, new) instead of atomic_cmpxchg_acquire(*ptr, old, new) == old in kexec_trylock(). x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg.

Seems it can simplify code even though on non-x86 arch, should we replace atomic_try_cmpxchg_acquire() with atomic_try_cmpxchg_acquire() in all similar places?

For this one,

Quoted 30 lines
Cc Eric Biederman <[email protected]>
---
 kernel/kexec_internal.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/kexec_internal.h +2 -1
index 2595defe8c0d..d35d9792402d 100644
@@ -23,7 +23,8 @@ int kimage_is_destination_range(struct kimage *image,
 extern atomic_t __kexec_lock;
 static inline bool kexec_trylock(void)
 {
-	return atomic_cmpxchg_acquire(&__kexec_lock, 0, 1) == 0;
+	int old = 0;
+	return atomic_try_cmpxchg_acquire(&__kexec_lock, &old, 1);
 }
 static inline void kexec_unlock(void)
 {
Signature · 7 lines
2.42.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#2 lore raw
Quoted 10 lines · Baoquan He
Quoted 4 lines · Uros Bizjak

Use atomic_try_cmpxchg_acquire(*ptr, &old, new) instead of atomic_cmpxchg_acquire(*ptr, old, new) == old in kexec_trylock(). x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg.

Seems it can simplify code even though on non-x86 arch, should we replace atomic_try_cmpxchg_acquire() with atomic_try_cmpxchg_acquire() in all similar places?

Yes, the change is beneficial also for non-x86 architectures, please see analysis at thread [1]. I've been looking through the kernel sources for these places for quite some time, and I believe I have changed most of the places. The change is relatively straightforward, and immediately results in a better code.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 4 lines

For this one,

Thanks,
Uros.

#3 lore raw
Quoted 12 lines · Uros Bizjak
Quoted 10 lines · Baoquan He
Quoted 4 lines · Uros Bizjak

Use atomic_try_cmpxchg_acquire(*ptr, &old, new) instead of atomic_cmpxchg_acquire(*ptr, old, new) == old in kexec_trylock(). x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg.

Seems it can simplify code even though on non-x86 arch, should we replace atomic_try_cmpxchg_acquire() with atomic_try_cmpxchg_acquire() in all similar places?

Quoted 7 lines

Yes, the change is beneficial also for non-x86 architectures, please see analysis at thread [1]. I've been looking through the kernel sources for these places for quite some time, and I believe I have changed most of the places. The change is relatively straightforward, and immediately results in a better code.

[1] https://lore.kernel.org/lkml/[email protected]/

Good to know, thanks for telling.