kernel/kexec_internal.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
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.
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Eric Biederman <ebiederm@xmission.com>
---
kernel/kexec_internal.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/kernel/kexec_internal.h b/kernel/kexec_internal.h
index 2595defe8c0d..d35d9792402d 100644
--- a/kernel/kexec_internal.h
+++ b/kernel/kexec_internal.h
@@ -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)
{
--
2.42.0
On 07/19/24 at 12:38pm, Uros Bizjak wrote:
> 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,
Acked-by: Baoquan He <bhe@redhat.com>
>
> Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
> Cc: Eric Biederman <ebiederm@xmission.com>
> ---
> kernel/kexec_internal.h | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/kexec_internal.h b/kernel/kexec_internal.h
> index 2595defe8c0d..d35d9792402d 100644
> --- a/kernel/kexec_internal.h
> +++ b/kernel/kexec_internal.h
> @@ -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)
> {
> --
> 2.42.0
>
>
> _______________________________________________
> kexec mailing list
> kexec@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/kexec
>
On Mon, Jul 22, 2024 at 5:09 AM Baoquan He <bhe@redhat.com> wrote: > > On 07/19/24 at 12:38pm, Uros Bizjak wrote: > > 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/871qwgmqws.fsf@mpe.ellerman.id.au/ > > For this one, > > Acked-by: Baoquan He <bhe@redhat.com> Thanks, Uros.
On 07/22/24 at 10:53am, Uros Bizjak wrote: > On Mon, Jul 22, 2024 at 5:09 AM Baoquan He <bhe@redhat.com> wrote: > > > > On 07/19/24 at 12:38pm, Uros Bizjak wrote: > > > 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/871qwgmqws.fsf@mpe.ellerman.id.au/ Good to know, thanks for telling.
© 2016 - 2025 Red Hat, Inc.