Skip to contentSkip to conversation

locking/qrwlock: queued_write_lock_slowpath() cleanup

PATCH Applied Waiman Long 5 participants 5 messages Apr 26, 2021 – May 6, 2021 Locking primitives

lore
Share & tools
lore

Waiman Long

author reviewer
#0 lore raw

PATCH 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
index b94f3831e963..ec36b73f4733 100644
@@ -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
#1 lore raw
Quoted 28 lines · Waiman Long

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 +0 -2
index b94f3831e963..ec36b73f4733 100644
@@ -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

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Waiman Long
Quoted 11 lines · Waiman Long

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.

Quoted 13 lines
---
 kernel/locking/qrwlock.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/locking/qrwlock.c +0 -0
index b94f3831e963..ec36b73f4733 100644
@@ -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

maintainer
#3 lore raw
Quoted 13 lines · Will Deacon
Quoted 11 lines · Waiman Long

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.

Quoted 6 lines

Right, there's no functional change here.

Quoted 2 lines

Thanks!

merge bot Applied
#4 lore raw

[tip: locking/urgent] locking/qrwlock: Cleanup queued_write_lock_slowpath()

In reply to Waiman Long
The 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
index b94f383..ec36b73 100644
@@ -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 {