Skip to contentSkip to conversation

locking/rwsem: Avoid double checking before try acquiring write lock

PATCH In review Jason Low 3 participants 4 messages Sep 17, 2014 – Oct 3, 2014 Locking primitives

lore
Share & tools
lore

Jason Low

author
#0 lore raw

PATCH v3 1/1 locking/rwsem: Avoid double checking before try acquiring write lock

Commit 9b0fc9c09f1b checks for if there are known active lockers in order to avoid write trylocking using expensive cmpxchg() when it likely wouldn't get the lock.

However, a subsequent patch was added such that we directly check for sem->count == RWSEM_WAITING_BIAS right before trying that cmpxchg(). Thus, commit 9b0fc9c09f1b now just adds overhead. This patch modifies it so that we only do a check for if count == RWSEM_WAITING_BIAS.

Also, add a comment on why we do an "extra check" of count before the cmpxchg().

Cc Peter Hurley <[email protected]>
Cc Tim Chen <[email protected]>
---
 kernel/locking/rwsem-xadd.c |   20 +++++++++++---------
 1 files changed, 11 insertions(+), 9 deletions(-)
kernel/locking/rwsem-xadd.c +11 -9
index 12166ec..7628c3f 100644
@@ -250,16 +250,18 @@ EXPORT_SYMBOL(rwsem_down_read_failed);
 
 static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem)
 {
-	if (!(count & RWSEM_ACTIVE_MASK)) {
-		/* try acquiring the write lock */
-		if (sem->count == RWSEM_WAITING_BIAS &&
-		    cmpxchg(&sem->count, RWSEM_WAITING_BIAS,
-			    RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) {
-			if (!list_is_singular(&sem->wait_list))
-				rwsem_atomic_update(RWSEM_WAITING_BIAS, sem);
-			return true;
-		}
+	/*
+	 * Try acquiring the write lock. Check count first in order
+	 * to reduce unnecessary expensive cmpxchg() operations.
+	 */
+	if (count == RWSEM_WAITING_BIAS &&
+	    cmpxchg(&sem->count, RWSEM_WAITING_BIAS,
+		    RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) {
+		if (!list_is_singular(&sem->wait_list))
+			rwsem_atomic_update(RWSEM_WAITING_BIAS, sem);
+		return true;
 	}
+
 	return false;
 }
 
Signature · 1 line
1.7.1
Acked-by on v3 1/1
#1 lore raw
Quoted 3 lines · Jason Low

Commit 9b0fc9c09f1b checks for if there are known active lockers in order to avoid write trylocking using expensive cmpxchg() when it likely wouldn't get the lock.

Ah, I remember you had this one in your queue for some time :)

Quoted 8 lines

However, a subsequent patch was added such that we directly check for sem->count == RWSEM_WAITING_BIAS right before trying that cmpxchg(). Thus, commit 9b0fc9c09f1b now just adds overhead. This patch modifies it so that we only do a check for if count == RWSEM_WAITING_BIAS.

Also, add a comment on why we do an "extra check" of count before the cmpxchg().

heh, so people don't try to remove the "redundant" check!

Quoted 2 lines
Cc Peter Hurley <[email protected]>
Cc Tim Chen <[email protected]>
Quoted 1 line
author
#2 lore raw
Quoted 13 lines · Davidlohr Bueso
Quoted 3 lines · Jason Low

Commit 9b0fc9c09f1b checks for if there are known active lockers in order to avoid write trylocking using expensive cmpxchg() when it likely wouldn't get the lock.

Ah, I remember you had this one in your queue for some time :)

Quoted 6 lines

However, a subsequent patch was added such that we directly check for sem->count == RWSEM_WAITING_BIAS right before trying that cmpxchg(). Thus, commit 9b0fc9c09f1b now just adds overhead. This patch modifies it so that we only do a check for if count == RWSEM_WAITING_BIAS.

Quoted 4 lines
Quoted 2 lines

Also, add a comment on why we do an "extra check" of count before the cmpxchg().

heh, so people don't try to remove the "redundant" check!

Right, I've already seen at least 1 such patch :)

Quoted 4 lines
Quoted 2 lines
Cc Peter Hurley <[email protected]>
Cc Tim Chen <[email protected]>

Thanks!

#3 lore raw
In reply to Jason Low
locking/rwsem: Avoid double checking before try acquiring write lock
Commit-ID:  debfab74e453f079cd8b12b0604387a8c510ef3a
Gitweb:     http://git.kernel.org/tip/debfab74e453f079cd8b12b0604387a8c510ef3a
Author:     Jason Low <[email protected]>
AuthorDate: Tue, 16 Sep 2014 17:16:57 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 3 Oct 2014 06:09:29 +0200

locking/rwsem: Avoid double checking before try acquiring write lock

Commit 9b0fc9c09f1b ("rwsem: skip initial trylock in rwsem_down_write_failed") checks for if there are known active lockers in order to avoid write trylocking using expensive cmpxchg() when it likely wouldn't get the lock.

However, a subsequent patch was added such that we directly check for sem->count == RWSEM_WAITING_BIAS right before trying that cmpxchg().

Thus, commit 9b0fc9c09f1b now just adds overhead.

This patch modifies it so that we only do a check for if count == RWSEM_WAITING_BIAS.

Also, add a comment on why we do an "extra check" of count before the cmpxchg().

---
 kernel/locking/rwsem-xadd.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
kernel/locking/rwsem-xadd.c +11 -9
index 12166ec..7628c3f 100644
@@ -250,16 +250,18 @@ EXPORT_SYMBOL(rwsem_down_read_failed);
 
 static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem)
 {
-	if (!(count & RWSEM_ACTIVE_MASK)) {
-		/* try acquiring the write lock */
-		if (sem->count == RWSEM_WAITING_BIAS &&
-		    cmpxchg(&sem->count, RWSEM_WAITING_BIAS,
-			    RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) {
-			if (!list_is_singular(&sem->wait_list))
-				rwsem_atomic_update(RWSEM_WAITING_BIAS, sem);
-			return true;
-		}
+	/*
+	 * Try acquiring the write lock. Check count first in order
+	 * to reduce unnecessary expensive cmpxchg() operations.
+	 */
+	if (count == RWSEM_WAITING_BIAS &&
+	    cmpxchg(&sem->count, RWSEM_WAITING_BIAS,
+		    RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) {
+		if (!list_is_singular(&sem->wait_list))
+			rwsem_atomic_update(RWSEM_WAITING_BIAS, sem);
+		return true;
 	}
+
 	return false;
 }