lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <feb68b86-899e-42a2-a680-4a73366e540d@redhat.com>
Date: Thu, 21 Mar 2024 19:52:21 -0400
From: Waiman Long <longman@...hat.com>
To: Uros Bizjak <ubizjak@...il.com>, linux-kernel@...r.kernel.org
Cc: Peter Zijlstra <peterz@...radead.org>, Ingo Molnar <mingo@...hat.com>,
 Will Deacon <will@...nel.org>, Boqun Feng <boqun.feng@...il.com>
Subject: Re: [PATCH] locking/qspinlock: Use atomic_try_cmpxchg_relaxed() in
 xchg_tail()

On 3/21/24 15:52, Uros Bizjak wrote:
> Use atomic_try_cmpxchg_relaxed(*ptr, &old, new) instead of
> atomic_cmpxchg_relaxed (*ptr, old, new) == old in xchg_tail().
> x86 CMPXCHG instruction returns success in ZF flag,
> so this change saves a compare after cmpxchg.
>
> No functional change intended.
>
> Signed-off-by: Uros Bizjak <ubizjak@...il.com>
> Cc: Peter Zijlstra <peterz@...radead.org>
> Cc: Ingo Molnar <mingo@...hat.com>
> Cc: Will Deacon <will@...nel.org>
> Cc: Waiman Long <longman@...hat.com>
> Cc: Boqun Feng <boqun.feng@...il.com>
> ---
>   kernel/locking/qspinlock.c | 13 +++++--------
>   1 file changed, 5 insertions(+), 8 deletions(-)
>
> diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c
> index ebe6b8ec7cb3..1df5fef8a656 100644
> --- a/kernel/locking/qspinlock.c
> +++ b/kernel/locking/qspinlock.c
> @@ -220,21 +220,18 @@ static __always_inline void clear_pending_set_locked(struct qspinlock *lock)
>    */
>   static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail)
>   {
> -	u32 old, new, val = atomic_read(&lock->val);
> +	u32 old, new;
>   
> -	for (;;) {
> -		new = (val & _Q_LOCKED_PENDING_MASK) | tail;
> +	old = atomic_read(&lock->val);
> +	do {
> +		new = (old & _Q_LOCKED_PENDING_MASK) | tail;
>   		/*
>   		 * We can use relaxed semantics since the caller ensures that
>   		 * the MCS node is properly initialized before updating the
>   		 * tail.
>   		 */
> -		old = atomic_cmpxchg_relaxed(&lock->val, val, new);
> -		if (old == val)
> -			break;
> +	} while (!atomic_try_cmpxchg_relaxed(&lock->val, &old, new));
>   
> -		val = old;
> -	}
>   	return old;
>   }
>   #endif /* _Q_PENDING_BITS == 8 */

LGTM, note that this xchg_tail() variant is not used in all the distros 
that I am aware of as it requires NR_CPUS >= 16k.

Reviewed-by: Waiman Long <longman@...hat.com>



Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ