Make the code more readable by replacing the atomic_cmpxchg_acquire()
by an equivalent atomic_try_cmpxchg_acquire() and change atomic_add()
to atomic_or().
For architectures that use qrwlock, I do not find one that has an
atomic_add() defined but not an atomic_or(). I guess it should be fine
by changing atomic_add() to atomic_or().
Note that the previous use of atomic_add() isn't wrong as only one
writer that is the wait_lock owner can set the waiting flag and the
flag will be cleared later on when acquiring the write lock.
Suggested-by: Linus Torvalds <[email protected]>
Signed-off-by: Waiman Long <[email protected]>
---
kernel/locking/qrwlock.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/kernel/locking/qrwlock.c b/kernel/locking/qrwlock.c
index b94f3831e963..ec36b73f4733 100644
--- a/kernel/locking/qrwlock.c
+++ b/kernel/locking/qrwlock.c
@@ -66,12 +66,12 @@ void queued_write_lock_slowpath(struct qrwlock *lock)
arch_spin_lock(&lock->wait_lock);
/* Try to acquire the lock directly if no reader is present */
- if (!atomic_read(&lock->cnts) &&
- (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
+ if (!(cnts = atomic_read(&lock->cnts)) &&
+ atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED))
goto unlock;
/* Set the waiting flag to notify readers that a writer is pending */
- atomic_add(_QW_WAITING, &lock->cnts);
+ atomic_or(_QW_WAITING, &lock->cnts);
/* When no more readers or writers, set the locked flag */
do {
--
2.18.1
On 4/26/21 2:50 PM, Waiman Long wrote:
> Make the code more readable by replacing the atomic_cmpxchg_acquire()
> by an equivalent atomic_try_cmpxchg_acquire() and change atomic_add()
> to atomic_or().
>
> For architectures that use qrwlock, I do not find one that has an
> atomic_add() defined but not an atomic_or(). I guess it should be fine
> by changing atomic_add() to atomic_or().
>
> Note that the previous use of atomic_add() isn't wrong as only one
> writer that is the wait_lock owner can set the waiting flag and the
> flag will be cleared later on when acquiring the write lock.
>
> Suggested-by: Linus Torvalds <[email protected]>
> Signed-off-by: Waiman Long <[email protected]>
> ---
> kernel/locking/qrwlock.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/locking/qrwlock.c b/kernel/locking/qrwlock.c
> index b94f3831e963..ec36b73f4733 100644
> --- a/kernel/locking/qrwlock.c
> +++ b/kernel/locking/qrwlock.c
> @@ -66,12 +66,12 @@ void queued_write_lock_slowpath(struct qrwlock *lock)
> arch_spin_lock(&lock->wait_lock);
>
> /* Try to acquire the lock directly if no reader is present */
> - if (!atomic_read(&lock->cnts) &&
> - (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
> + if (!(cnts = atomic_read(&lock->cnts)) &&
> + atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED))
> goto unlock;
>
> /* Set the waiting flag to notify readers that a writer is pending */
> - atomic_add(_QW_WAITING, &lock->cnts);
> + atomic_or(_QW_WAITING, &lock->cnts);
>
> /* When no more readers or writers, set the locked flag */
> do {
Sorry, I missed the "v2" in the title.
Cheers,
Longman
On Mon, Apr 26, 2021 at 02:50:17PM -0400, Waiman Long wrote:
> Make the code more readable by replacing the atomic_cmpxchg_acquire()
> by an equivalent atomic_try_cmpxchg_acquire() and change atomic_add()
> to atomic_or().
>
> For architectures that use qrwlock, I do not find one that has an
> atomic_add() defined but not an atomic_or(). I guess it should be fine
> by changing atomic_add() to atomic_or().
>
> Note that the previous use of atomic_add() isn't wrong as only one
> writer that is the wait_lock owner can set the waiting flag and the
> flag will be cleared later on when acquiring the write lock.
Right, there's no functional change here.
> Suggested-by: Linus Torvalds <[email protected]>
> Signed-off-by: Waiman Long <[email protected]>
> ---
> kernel/locking/qrwlock.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/locking/qrwlock.c b/kernel/locking/qrwlock.c
> index b94f3831e963..ec36b73f4733 100644
> --- a/kernel/locking/qrwlock.c
> +++ b/kernel/locking/qrwlock.c
> @@ -66,12 +66,12 @@ void queued_write_lock_slowpath(struct qrwlock *lock)
> arch_spin_lock(&lock->wait_lock);
>
> /* Try to acquire the lock directly if no reader is present */
> - if (!atomic_read(&lock->cnts) &&
> - (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
> + if (!(cnts = atomic_read(&lock->cnts)) &&
> + atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED))
> goto unlock;
>
> /* Set the waiting flag to notify readers that a writer is pending */
> - atomic_add(_QW_WAITING, &lock->cnts);
> + atomic_or(_QW_WAITING, &lock->cnts);
Acked-by: Will Deacon <[email protected]>
Will
On Tue, Apr 27, 2021 at 08:56:59AM +0100, Will Deacon wrote:
> On Mon, Apr 26, 2021 at 02:50:17PM -0400, Waiman Long wrote:
> > Make the code more readable by replacing the atomic_cmpxchg_acquire()
> > by an equivalent atomic_try_cmpxchg_acquire() and change atomic_add()
> > to atomic_or().
> >
> > For architectures that use qrwlock, I do not find one that has an
> > atomic_add() defined but not an atomic_or(). I guess it should be fine
> > by changing atomic_add() to atomic_or().
> >
> > Note that the previous use of atomic_add() isn't wrong as only one
> > writer that is the wait_lock owner can set the waiting flag and the
> > flag will be cleared later on when acquiring the write lock.
>
> Right, there's no functional change here.
>
> > Suggested-by: Linus Torvalds <[email protected]>
> > Signed-off-by: Waiman Long <[email protected]>
> Acked-by: Will Deacon <[email protected]>
Thanks!
The following commit has been merged into the locking/urgent branch of tip:
Commit-ID: 28ce0e70ecc30cc7d558a0304e6b816d70848f9a
Gitweb: https://git.kernel.org/tip/28ce0e70ecc30cc7d558a0304e6b816d70848f9a
Author: Waiman Long <[email protected]>
AuthorDate: Mon, 26 Apr 2021 14:50:17 -04:00
Committer: Peter Zijlstra <[email protected]>
CommitterDate: Thu, 06 May 2021 15:33:49 +02:00
locking/qrwlock: Cleanup queued_write_lock_slowpath()
Make the code more readable by replacing the atomic_cmpxchg_acquire()
by an equivalent atomic_try_cmpxchg_acquire() and change atomic_add()
to atomic_or().
For architectures that use qrwlock, I do not find one that has an
atomic_add() defined but not an atomic_or(). I guess it should be fine
by changing atomic_add() to atomic_or().
Note that the previous use of atomic_add() isn't wrong as only one
writer that is the wait_lock owner can set the waiting flag and the
flag will be cleared later on when acquiring the write lock.
Suggested-by: Linus Torvalds <[email protected]>
Signed-off-by: Waiman Long <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Acked-by: Will Deacon <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
---
kernel/locking/qrwlock.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/kernel/locking/qrwlock.c b/kernel/locking/qrwlock.c
index b94f383..ec36b73 100644
--- a/kernel/locking/qrwlock.c
+++ b/kernel/locking/qrwlock.c
@@ -66,12 +66,12 @@ void queued_write_lock_slowpath(struct qrwlock *lock)
arch_spin_lock(&lock->wait_lock);
/* Try to acquire the lock directly if no reader is present */
- if (!atomic_read(&lock->cnts) &&
- (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
+ if (!(cnts = atomic_read(&lock->cnts)) &&
+ atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED))
goto unlock;
/* Set the waiting flag to notify readers that a writer is pending */
- atomic_add(_QW_WAITING, &lock->cnts);
+ atomic_or(_QW_WAITING, &lock->cnts);
/* When no more readers or writers, set the locked flag */
do {