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(). I add a comment to state that we can change it back to atomic_add() if there is an architecture that has a more performant atomic_add() than an atomic_or().
--- kernel/locking/qrwlock.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
kernel/locking/qrwlock.c +12 -4
@@ -60,18 +60,26 @@ EXPORT_SYMBOL(queued_read_lock_slowpath);*/ void queued_write_lock_slowpath (struct qrwlock *lock) { -int cnts; +int cnts =0 ;/* Put the writer into the wait queue */ 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 )) +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); +/* +* Set the waiting flag to notify readers that a writer is pending +* +* As only one writer who is the wait_lock owner can set the waiting +* flag which will be cleared later on when acquiring the write lock, +* we can easily replace atomic_or() by an atomic_add() if there is +* an architecture where an atomic_add() performs better than an +* atomic_or(). +*/ +atomic_or (_QW_WAITING, &lock->cnts);/* When no more readers or writers, set the locked flag */ do {
Signature · 1 line
2.18.1
Peter Zijlstra
maintainerkernel/locking/qrwlock.c EXPORT_SYMBOL(queued_read_lock_slowpath); · line 71
Quoted 13 lines · Waiman Long
void
queued_write_lock_slowpath(struct qrwlock *lock){kernel/locking/qrwlock.c +2
-2-int cnts; +int cnts =0 ;/* Put the writer into the wait queue */ 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 )) +atomic_try_cmpxchg_acquire (&lock->cnts, &cnts, _QW_LOCKED)) goto unlock;
Would not something like:
if (!(cnts = atomic_read(&lock->cnts)) && atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED) goto unlock;
Be clearer?
kernel/locking/qrwlock.c EXPORT_SYMBOL(queued_read_lock_slowpath); · line 80
Quoted 11 lines
kernel/locking/qrwlock.c +8
-2-/* Set the waiting flag to notify readers that a writer is pending */ -atomic_add (_QW_WAITING, &lock->cnts); +/* +* Set the waiting flag to notify readers that a writer is pending +* +* As only one writer who is the wait_lock owner can set the waiting +* flag which will be cleared later on when acquiring the write lock, +* we can easily replace atomic_or() by an atomic_add() if there is +* an architecture where an atomic_add() performs better than an +* atomic_or().
That might be a little overboard on the comment, but sure :-) I don't think there's any arch that doesn't have atomic_or(), like I wrote elsewhere, the one that's often an issue is atomic_fetch_or().
Quoted 2 lines
kernel/locking/qrwlock.c +2
-0+*/ +atomic_or (_QW_WAITING, &lock->cnts);
Quoted 17 lines · Peter Zijlstra
kernel/locking/qrwlock.c EXPORT_SYMBOL(queued_read_lock_slowpath); · line 71
Quoted 13 lines · Waiman Long
void queued_write_lock_slowpath(struct qrwlock *lock) {kernel/locking/qrwlock.c +2
-2-int cnts; +int cnts =0 ;/* Put the writer into the wait queue */ 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 )) +atomic_try_cmpxchg_acquire (&lock->cnts, &cnts, _QW_LOCKED)) goto unlock;Would not something like:
Quoted 5 lines
if (!(cnts = atomic_read(&lock->cnts)) && atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED) goto unlock;Be clearer?
That works for me too. It is equivalent anyway.
Quoted 16 lines
kernel/locking/qrwlock.c EXPORT_SYMBOL(queued_read_lock_slowpath); · line 80
Quoted 12 lines
kernel/locking/qrwlock.c +8
-2-/* Set the waiting flag to notify readers that a writer is pending */ -atomic_add (_QW_WAITING, &lock->cnts); +/* +* Set the waiting flag to notify readers that a writer is pending +* +* As only one writer who is the wait_lock owner can set the waiting +* flag which will be cleared later on when acquiring the write lock, +* we can easily replace atomic_or() by an atomic_add() if there is +* an architecture where an atomic_add() performs better than an +* atomic_or(). That might be a little overboard on the comment, but sure :-) I don't think there's any arch that doesn't have
atomic_or(), like I wrote elsewhere, the one that's often an issue isatomic_fetch_or().
I was not sure as I didn't look at other archs that hadn't used qrwlock yet. Given what you said, I will remove the comment.
Cheers,
Longman