locking/qrwlock: queued_write_lock_slowpath() cleanup
Waiman Long
reviewerPATCH 1/1 locking/qrwlock: queued_write_lock_slowpath() cleanup
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.
--- kernel/locking/qrwlock.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/locking/qrwlock.c +3 -3
@@ -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 {
Signature · 1 line
2.18.1
Quoted 28 lines · Waiman Long
Make the code more readable by replacing the
atomic_cmpxchg_acquire()by an equivalentatomic_try_cmpxchg_acquire()and changeatomic_add()toatomic_or().For architectures that use qrwlock, I do not find one that has an
atomic_add()defined but not anatomic_or(). I guess it should be fine by changingatomic_add()toatomic_or().Note that the previous use of
atomic_add()isn't wrong as only one writer that is thewait_lockowner 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(-)kernel/locking/qrwlock.c +0
-2@@ -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 ))
kernel/locking/qrwlock.c void queued_write_lock_slowpath(struct qrwlock *lock) · line 77
Quoted 10 lines
kernel/locking/qrwlock.c +3
-1+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
Quoted 11 lines · Waiman Long
Make the code more readable by replacing the
atomic_cmpxchg_acquire()by an equivalentatomic_try_cmpxchg_acquire()and changeatomic_add()toatomic_or().For architectures that use qrwlock, I do not find one that has an
atomic_add()defined but not anatomic_or(). I guess it should be fine by changingatomic_add()toatomic_or().Note that the previous use of
atomic_add()isn't wrong as only one writer that is thewait_lockowner 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.
Quoted 13 lines
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(-)kernel/locking/qrwlock.c +0
-0@@ -66,12 +66,12 @@ void queued_write_lock_slowpath(struct qrwlock *lock)arch_spin_lock (&lock->wait_lock);
kernel/locking/qrwlock.c void queued_write_lock_slowpath(struct qrwlock *lock) · line 74
Quoted 10 lines
kernel/locking/qrwlock.c +3
-3/* 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);
Will
Peter Zijlstra
maintainerQuoted 13 lines · Will Deacon
Quoted 11 lines · Waiman Long
Make the code more readable by replacing the
atomic_cmpxchg_acquire()by an equivalentatomic_try_cmpxchg_acquire()and changeatomic_add()toatomic_or().For architectures that use qrwlock, I do not find one that has an
atomic_add()defined but not anatomic_or(). I guess it should be fine by changingatomic_add()toatomic_or().Note that the previous use of
atomic_add()isn't wrong as only one writer that is thewait_lockowner can set the waiting flag and the flag will be cleared later on when acquiring the write lock.
Quoted 6 lines
Right, there's no functional change here.
Quoted 2 lines
Suggested-by Linus Torvalds <[email protected]>Signed-off-by Waiman Long <[email protected]>Acked-by Will Deacon <[email protected]>
Thanks!
[tip: locking/urgent] locking/qrwlock: Cleanup queued_write_lock_slowpath()
In reply to Waiman LongThe following commit has been merged into the locking/urgent branch of tip:
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.
--- kernel/locking/qrwlock.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/locking/qrwlock.c +3 -3
@@ -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 {