Skip to contentSkip to conversation

locking/rwlocks: clean up of qrwlock

PATCH In review Baoquan He 3 participants 7 messages Dec 16, 2014 – Jan 6, 2015 Locking primitives

lore
Share & tools
lore

Baoquan He

author
#0 lore raw

PATCH 1/1 locking/rwlocks: clean up of qrwlock

In queue_read_lock_slowpath, when writer count becomes 0, we need increment the read count and get the lock. Then need call rspin_until_writer_unlock to check again if an incoming writer steals the lock in the gap. But in rspin_until_writer_unlock it only checks the writer count, namely low 8 bit of lock->cnts, no need to subtract the reader count unit specifically. So remove that subtraction to make it clearer, rspin_until_writer_unlock just takes the actual lock->cnts as the 2nd argument.

And also change the code comment in queue_write_lock_slowpath to make it more exact and explicit.

---
 kernel/locking/qrwlock.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/locking/qrwlock.c +4 -4
index f956ede..ae66c10 100644
@@ -76,7 +76,7 @@ void queue_read_lock_slowpath(struct qrwlock *lock)
 	while (atomic_read(&lock->cnts) & _QW_WMASK)
 		cpu_relax_lowlatency();
 
-	cnts = atomic_add_return(_QR_BIAS, &lock->cnts) - _QR_BIAS;
+	cnts = atomic_add_return(_QR_BIAS, &lock->cnts);
 	rspin_until_writer_unlock(lock, cnts);
 
 	/*
@@ -97,14 +97,14 @@ void queue_write_lock_slowpath(struct qrwlock *lock)
 	/* Put the writer into the wait queue */
 	arch_spin_lock(&lock->lock);
 
-	/* Try to acquire the lock directly if no reader is present */
+	/* Try to acquire the lock directly if no reader and writer is present */
 	if (!atomic_read(&lock->cnts) &&
 	    (atomic_cmpxchg(&lock->cnts, 0, _QW_LOCKED) == 0))
 		goto unlock;
 
 	/*
-	 * Set the waiting flag to notify readers that a writer is pending,
-	 * or wait for a previous writer to go away.
+	 * Wait for a previous writer to go away, then set the waiting flag to
+	 * notify readers that a writer is pending.
 	 */
 	for (;;) {
 		cnts = atomic_read(&lock->cnts);
Signature · 1 line
1.8.5.3
maintainer
#1 lore raw
Quoted 22 lines · Baoquan He

In queue_read_lock_slowpath, when writer count becomes 0, we need increment the read count and get the lock. Then need call rspin_until_writer_unlock to check again if an incoming writer steals the lock in the gap. But in rspin_until_writer_unlock it only checks the writer count, namely low 8 bit of lock->cnts, no need to subtract the reader count unit specifically. So remove that subtraction to make it clearer, rspin_until_writer_unlock just takes the actual lock->cnts as the 2nd argument.

And also change the code comment in queue_write_lock_slowpath to make it more exact and explicit.

---
 kernel/locking/qrwlock.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/locking/qrwlock.c +0 -0
index f956ede..ae66c10 100644

kernel/locking/qrwlock.c void queue_read_lock_slowpath(struct qrwlock *lock) · line 80

Quoted 6 lines
kernel/locking/qrwlock.c +1 -1
@@ -76,7 +76,7 @@ void queue_read_lock_slowpath(struct qrwlock *lock)
 	while (atomic_read(&lock->cnts) & _QW_WMASK)
 		cpu_relax_lowlatency();
 
-	cnts = atomic_add_return(_QR_BIAS, &lock->cnts) - _QR_BIAS;
+	cnts = atomic_add_return(_QR_BIAS, &lock->cnts);
 	rspin_until_writer_unlock(lock, cnts);

Did you actually look at the ASM generated? I suspect your change makes it bigger.

author
#2 lore raw
Quoted 26 lines · Peter Zijlstra
Quoted 25 lines · Baoquan He

In queue_read_lock_slowpath, when writer count becomes 0, we need increment the read count and get the lock. Then need call rspin_until_writer_unlock to check again if an incoming writer steals the lock in the gap. But in rspin_until_writer_unlock it only checks the writer count, namely low 8 bit of lock->cnts, no need to subtract the reader count unit specifically. So remove that subtraction to make it clearer, rspin_until_writer_unlock just takes the actual lock->cnts as the 2nd argument.

And also change the code comment in queue_write_lock_slowpath to make it more exact and explicit.

---
 kernel/locking/qrwlock.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/locking/qrwlock.c +0 -0
index f956ede..ae66c10 100644
@@ -76,7 +76,7 @@ void queue_read_lock_slowpath(struct qrwlock *lock)
 	while (atomic_read(&lock->cnts) & _QW_WMASK)
 		cpu_relax_lowlatency();
 
Quoted 6 lines

kernel/locking/qrwlock.c void queue_read_lock_slowpath(struct qrwlock *lock) · line 80

Quoted 3 lines
kernel/locking/qrwlock.c +1 -1
-	cnts = atomic_add_return(_QR_BIAS, &lock->cnts) - _QR_BIAS;
+	cnts = atomic_add_return(_QR_BIAS, &lock->cnts);
 	rspin_until_writer_unlock(lock, cnts);

Did you actually look at the ASM generated? I suspect your change makes it bigger.

It does make it bigger. But it doesn't matter. Because in rspin_until_writer_unlock it only compqre (cnts & _QW_WMASK) with _QW_LOCKED. So using incremented reader count doesn't impact the result. Anyway it will get the actual lock->cnts in rspin_until_writer_unlock in next loop. I can't see why we need subtract that reader count increment specifically.

When I read this code, thought there's some special usage. Finally I realized it doesn't have special usage, and doesn't have to do that.

Acked-by
#3 lore raw
Quoted 40 lines · Baoquan He
Quoted 26 lines · Peter Zijlstra
Quoted 25 lines · Baoquan He

In queue_read_lock_slowpath, when writer count becomes 0, we need increment the read count and get the lock. Then need call rspin_until_writer_unlock to check again if an incoming writer steals the lock in the gap. But in rspin_until_writer_unlock it only checks the writer count, namely low 8 bit of lock->cnts, no need to subtract the reader count unit specifically. So remove that subtraction to make it clearer, rspin_until_writer_unlock just takes the actual lock->cnts as the 2nd argument.

And also change the code comment in queue_write_lock_slowpath to make it more exact and explicit.

---
  kernel/locking/qrwlock.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
kernel/locking/qrwlock.c +0 -0
index f956ede..ae66c10 100644
@@ -76,7 +76,7 @@ void queue_read_lock_slowpath(struct qrwlock *lock)
  	while (atomic_read(&lock->cnts)&  _QW_WMASK)
  		cpu_relax_lowlatency();
 
Quoted 5 lines

kernel/locking/qrwlock.c void queue_read_lock_slowpath(struct qrwlock *lock) · line 80

Quoted 3 lines
kernel/locking/qrwlock.c +1 -1
-	cnts = atomic_add_return(_QR_BIAS,&lock->cnts) - _QR_BIAS;
+	cnts = atomic_add_return(_QR_BIAS,&lock->cnts);
  	rspin_until_writer_unlock(lock, cnts);

Did you actually look at the ASM generated? I suspect your change makes it bigger.

It does make it bigger. But it doesn't matter. Because in rspin_until_writer_unlock it only compqre (cnts& _QW_WMASK) with _QW_LOCKED. So using incremented reader count doesn't impact the result. Anyway it will get the actual lock->cnts in rspin_until_writer_unlock in next loop. I can't see why we need subtract that reader count increment specifically.

Quoted 2 lines

When I read this code, thought there's some special usage. Finally I realized it doesn't have special usage, and doesn't have to do that.

The "- _QR_BIAS" expression was added to simulate xadd() which is present in x86, but not in some other architectures. There is no equivalent functionality in the set of atomic helper functions. Anyway, I have no objection to the change as it is in the slowpath.

author
#4 lore raw

Hi all,

Thanks for reviewing.

Do you have any other concerns about this small change?

Thanks
Baoquan

Quoted 56 lines · Waiman Long
Quoted 40 lines · Baoquan He
Quoted 26 lines · Peter Zijlstra
Quoted 25 lines · Baoquan He

In queue_read_lock_slowpath, when writer count becomes 0, we need increment the read count and get the lock. Then need call rspin_until_writer_unlock to check again if an incoming writer steals the lock in the gap. But in rspin_until_writer_unlock it only checks the writer count, namely low 8 bit of lock->cnts, no need to subtract the reader count unit specifically. So remove that subtraction to make it clearer, rspin_until_writer_unlock just takes the actual lock->cnts as the 2nd argument.

And also change the code comment in queue_write_lock_slowpath to make it more exact and explicit.

---
 kernel/locking/qrwlock.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/locking/qrwlock.c +0 -0
index f956ede..ae66c10 100644
@@ -76,7 +76,7 @@ void queue_read_lock_slowpath(struct qrwlock *lock)
 	while (atomic_read(&lock->cnts)&  _QW_WMASK)
 		cpu_relax_lowlatency();
 
Quoted 5 lines

kernel/locking/qrwlock.c void queue_read_lock_slowpath(struct qrwlock *lock) · line 80

Quoted 3 lines
kernel/locking/qrwlock.c +1 -1
-	cnts = atomic_add_return(_QR_BIAS,&lock->cnts) - _QR_BIAS;
+	cnts = atomic_add_return(_QR_BIAS,&lock->cnts);
 	rspin_until_writer_unlock(lock, cnts);

Did you actually look at the ASM generated? I suspect your change makes it bigger.

It does make it bigger. But it doesn't matter. Because in rspin_until_writer_unlock it only compqre (cnts& _QW_WMASK) with _QW_LOCKED. So using incremented reader count doesn't impact the result. Anyway it will get the actual lock->cnts in rspin_until_writer_unlock in next loop. I can't see why we need subtract that reader count increment specifically.

Quoted 2 lines

When I read this code, thought there's some special usage. Finally I realized it doesn't have special usage, and doesn't have to do that.

The "- _QR_BIAS" expression was added to simulate xadd() which is present in x86, but not in some other architectures. There is no equivalent functionality in the set of atomic helper functions. Anyway, I have no objection to the change as it is in the slowpath.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

maintainer
#5 lore raw
Quoted 5 lines · Baoquan He

Hi all,

Thanks for reviewing.

Do you have any other concerns about this small change?

I just don't see any point in making the code worse for the 1 arch that actually uses it. If there were other archs using it and for them the code generation would be negatively impacted (they'd actually generate more core because of it) it might be worth looking at.

author
#6 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 5 lines · Baoquan He

Hi all,

Thanks for reviewing.

Do you have any other concerns about this small change?

I just don't see any point in making the code worse for the 1 arch that actually uses it. If there were other archs using it and for them the code generation would be negatively impacted (they'd actually generate more core because of it) it might be worth looking at.

Hi Peter,

It's OK if you don't like it. I am just a little confused about 2 things:

1) Waiman said "- _QR_BIAS" is used to simulate the xaddr which is only in x86. So removing it to make it be consistent with other ARCHs, why is it not good? And you said making the code worse, could you be more specific?

2) Previously you said this change will make it bigger, I thought you means the cnts. Am I correct? Or you mean other thing?

Just want to understand it clearly, please point out what's wrong about my understanding.