Skip to contentSkip to conversation

locking/qspinlock: Fix state-transition changes in comments

PATCH Superseded Qiuxu Zhuo 2 participants 9 messages May 6, 2023 – May 10, 2023 Locking primitives

lore
Share & tools
lore

Qiuxu Zhuo

author
#0 lore raw

PATCH 1/1 locking/qspinlock: Fix state-transition changes in comments

1. There may be concurrent locker CPUs to set the qspinlock pending bit.

The first CPU (called pending CPU) of these CPUs sets the pending bit to make the state transition (the qspinlock pending bit is set):

         0,0,* -> 0,1,*

The rest of these CPUs are queued to the MCS queue to make the state transition (the qspinlock tail is updated):

         0,1,* -> *,1,*

The pending CPU waits until the locker owner goes away to make the state transition (the qspinlock locked field is set to zero):

         *,1,* -> *,1,0

The pending CPU takes the ownership and clears the pending bit to make the state transition:

         *,1,0 -> *,0,1

2. The header of the MCS queue takes the ownership and calls set_locked() to make the state transition:

         *,*,0 -> *,*,1

Fix the state-transition changes above in the code comments accordingly.

---
 kernel/locking/qspinlock.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/locking/qspinlock.c +6 -4
index ebe6b8ec7cb3..efebbf19f887 100644
@@ -257,7 +257,7 @@ static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lo
  * set_locked - Set the lock bit and own the lock
  * @lock: Pointer to queued spinlock structure
  *
- * *,*,0 -> *,0,1
+ * *,*,0 -> *,*,1
  */
 static __always_inline void set_locked(struct qspinlock *lock)
 {
@@ -348,7 +348,7 @@ void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
 	/*
 	 * trylock || pending
 	 *
-	 * 0,0,* -> 0,1,* -> 0,0,1 pending, trylock
+	 * 0,0,* -> 0,1,* -> ... -> *,0,1 pending, trylock
 	 */
 	val = queued_fetch_set_pending_acquire(lock);
 
@@ -358,6 +358,8 @@ void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
 	 * Undo and queue; our setting of PENDING might have made the
 	 * n,0,0 -> 0,0,0 transition fail and it will now be waiting
 	 * on @next to become !NULL.
+	 *
+	 * 0,1,* -> *,1,*
 	 */
 	if (unlikely(val & ~_Q_LOCKED_MASK)) {
 
@@ -371,7 +373,7 @@ void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
 	/*
 	 * We're pending, wait for the owner to go away.
 	 *
-	 * 0,1,1 -> *,1,0
+	 * *,1,* -> *,1,0
 	 *
 	 * this wait loop must be a load-acquire such that we match the
 	 * store-release that clears the locked bit and create lock
@@ -385,7 +387,7 @@ void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
 	/*
 	 * take ownership and clear the pending bit.
 	 *
-	 * 0,1,0 -> 0,0,1
+	 * *,1,0 -> *,0,1
 	 */
 	clear_pending_set_locked(lock);
 	lockevent_inc(lock_pending);
Signature · 1 line
2.17.1
reviewer
#1 lore raw
Quoted 20 lines · Qiuxu Zhuo

1. There may be concurrent locker CPUs to set the qspinlock pending bit.

The first CPU (called pending CPU) of these CPUs sets the pending bit to make the state transition (the qspinlock pending bit is set):

0,0,* -> 0,1,*

The rest of these CPUs are queued to the MCS queue to make the state transition (the qspinlock tail is updated):

0,1,* -> *,1,*

The pending CPU waits until the locker owner goes away to make the state transition (the qspinlock locked field is set to zero):

*,1,* -> *,1,0

The pending CPU takes the ownership and clears the pending bit to make the state transition:

Quoted 6 lines

*,1,0 -> *,0,1

2. The header of the MCS queue takes the ownership and calls set_locked() to make the state transition:

*,*,0 -> *,*,1

That is not true. The pending bit owner has priority over the MCS queue head. So the pending bit must be 0 before the MCS queue head can take over the lock. So

    *,0,0 -> *,0,1

Quoted 13 lines

Fix the state-transition changes above in the code comments accordingly.

---
  kernel/locking/qspinlock.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
kernel/locking/qspinlock.c +0 -0
index ebe6b8ec7cb3..efebbf19f887 100644

kernel/locking/qspinlock.c static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lo · line 260

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
@@ -257,7 +257,7 @@ static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lo
   * set_locked - Set the lock bit and own the lock
   * @lock: Pointer to queued spinlock structure
   *
- * *,*,0 -> *,0,1
+ * *,*,0 -> *,*,1

set_locked() can only be called when it is sure that the pending bit isn't set.

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 351

Quoted 9 lines
kernel/locking/qspinlock.c +1 -1
   */
  static __always_inline void set_locked(struct qspinlock *lock)
  {
@@ -348,7 +348,7 @@ void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
  	/*
  	 * trylock || pending
  	 *
-	 * 0,0,* -> 0,1,* -> 0,0,1 pending, trylock
+	 * 0,0,* -> 0,1,* -> ... -> *,0,1 pending, trylock

By the time trylock is done, there may be entries in the queue. However, I doubt it helps by adding "..." in between possible multiple transitions.

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 362

Quoted 9 lines
kernel/locking/qspinlock.c +2 -0
  	 */
  	val = queued_fetch_set_pending_acquire(lock);
 
@@ -358,6 +358,8 @@ void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
  	 * Undo and queue; our setting of PENDING might have made the
  	 * n,0,0 -> 0,0,0 transition fail and it will now be waiting
  	 * on @next to become !NULL.
+	 *
+	 * 0,1,* -> *,1,*

There is already a "n,0,0 -> 0,0,0" above, adding a new one may just confuse people.

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 376

Quoted 9 lines
kernel/locking/qspinlock.c +1 -1
  	 */
  	if (unlikely(val & ~_Q_LOCKED_MASK)) {
 
@@ -371,7 +373,7 @@ void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
  	/*
  	 * We're pending, wait for the owner to go away.
  	 *
-	 * 0,1,1 -> *,1,0
+	 * *,1,* -> *,1,0

This refers to the wait loop. We don't need to wait if the owner has gone.

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 390

Quoted 9 lines
kernel/locking/qspinlock.c +1 -1
  	 *
  	 * this wait loop must be a load-acquire such that we match the
  	 * store-release that clears the locked bit and create lock
@@ -385,7 +387,7 @@ void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
  	/*
  	 * take ownership and clear the pending bit.
  	 *
-	 * 0,1,0 -> 0,0,1
+	 * *,1,0 -> *,0,1

That is the part that we can make the change in the transition diagram as noted.

Cheers,
Longman

#2 lore raw

Hi Wainman,

Thanks for your review. Please see the comments below.

Quoted 36 lines

From: Waiman Long <[email protected]>
Sent: Monday, May 8, 2023 11:29 PM
To: Zhuo, Qiuxu <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Will Deacon <[email protected]>
Cc: Boqun Feng <[email protected]>; [email protected]
Subject: Re: [PATCH 1/1] locking/qspinlock: Fix state-transition changes in comments

Quoted 25 lines · Qiuxu Zhuo

1. There may be concurrent locker CPUs to set the qspinlock pending bit.

The first CPU (called pending CPU) of these CPUs sets the pending bit to make the state transition (the qspinlock pending bit is set):

0,0,* -> 0,1,*

The rest of these CPUs are queued to the MCS queue to make the state transition (the qspinlock tail is updated):

0,1,* -> *,1,*

The pending CPU waits until the locker owner goes away to make the state transition (the qspinlock locked field is set to zero):

*,1,* -> *,1,0

The pending CPU takes the ownership and clears the pending bit to make the state transition:

*,1,0 -> *,0,1

2. The header of the MCS queue takes the ownership and calls set_locked() to make the state transition:

Quoted 7 lines
Quoted 1 line
          *,*,0 -> *,*,1

That is not true. The pending bit owner has priority over the MCS queue head. So the pending bit must be 0 before the MCS queue head can take over the lock. So

    *,0,0 -> *,0,1

Yes, the pending bit must be 0 before the header can take the lock.
But as the statement "There may be concurrent locker CPUs to set the qspinlock pending bit " at the beginning. So just after the header takes over the lock, there is also a possible concurrent locker CPU to set the pending bit. That means at this time point here, the pending bit could be either 0 or 1.

Quoted 25 lines

kernel/locking/qspinlock.c static __always_inline u32

Quoted 13 lines

Fix the state-transition changes above in the code comments accordingly.

---
  kernel/locking/qspinlock.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
kernel/locking/qspinlock.c +0 -0
index ebe6b8ec7cb3..efebbf19f887 100644
@@ -257,7 +257,7 @@ static __always_inline u32

queued_fetch_set_pending_acquire(struct qspinlock *lo

kernel/locking/qspinlock.c static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lo · line 260

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
   * set_locked - Set the lock bit and own the lock
   * @lock: Pointer to queued spinlock structure
   *
- * *,*,0 -> *,0,1
+ * *,*,0 -> *,*,1

set_locked() can only be called when it is sure that the pending bit isn't set.

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct

Quoted 4 lines
kernel/locking/qspinlock.c +0 -0
   */
  static __always_inline void set_locked(struct qspinlock *lock)
  {
@@ -348,7 +348,7 @@ void __lockfunc queued_spin_lock_slowpath(struct

qspinlock *lock, u32 val)

Quoted 9 lines

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 351

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
  	/*
  	 * trylock || pending
  	 *
-	 * 0,0,* -> 0,1,* -> 0,0,1 pending, trylock
+	 * 0,0,* -> 0,1,* -> ... -> *,0,1 pending, trylock

By the time trylock is done, there may be entries in the queue. However, I doubt it helps by adding "..." in between possible multiple transitions.

The added "..." means there could be entries in the queue before trylock done. This is just for making the state transitions more complete ;-). If you think it doesn't help, I can remove it in the next version.

Quoted 17 lines

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct

Quoted 4 lines
kernel/locking/qspinlock.c +0 -0
  	 */
  	val = queued_fetch_set_pending_acquire(lock);
 
@@ -358,6 +358,8 @@ void __lockfunc queued_spin_lock_slowpath(struct

qspinlock *lock, u32 val)

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 362

Quoted 5 lines
kernel/locking/qspinlock.c +2 -0
  	 * Undo and queue; our setting of PENDING might have made the
  	 * n,0,0 -> 0,0,0 transition fail and it will now be waiting
  	 * on @next to become !NULL.
+	 *
+	 * 0,1,* -> *,1,*

There is already a "n,0,0 -> 0,0,0" above, adding a new one may just confuse people.

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct

Quoted 4 lines
kernel/locking/qspinlock.c +0 -0
  	 */
  	if (unlikely(val & ~_Q_LOCKED_MASK)) {
 
@@ -371,7 +373,7 @@ void __lockfunc queued_spin_lock_slowpath(struct

qspinlock *lock, u32 val)

Quoted 7 lines

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 376

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
  	/*
  	 * We're pending, wait for the owner to go away.
  	 *
-	 * 0,1,1 -> *,1,0
+	 * *,1,* -> *,1,0

This refers to the wait loop. We don't need to wait if the owner has gone.

But just before we wait for the locked field, the locked field could be either 0 (the locker can release the lock at any time) or 1.

Quoted 6 lines

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct

Quoted 5 lines
kernel/locking/qspinlock.c +0 -0
  	 *
  	 * this wait loop must be a load-acquire such that we match the
  	 * store-release that clears the locked bit and create lock
@@ -385,7 +387,7 @@ void __lockfunc queued_spin_lock_slowpath(struct

qspinlock *lock, u32 val)

Quoted 8 lines

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 390

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
  	/*
  	 * take ownership and clear the pending bit.
  	 *
-	 * 0,1,0 -> 0,0,1
+	 * *,1,0 -> *,0,1

That is the part that we can make the change in the transition diagram as noted.

Sorry, I'm not clear about your request.
Did you mean just make the change "*,1,0 -> *,0,1" above in the transition diagram or all the changes above in the transition diagram?

Thanks!
-Qiuxu

Quoted 2 lines

Cheers,
Longman

reviewer
#3 lore raw
Quoted 45 lines · Qiuxu

Hi Wainman,

Thanks for your review. Please see the comments below.

Quoted 41 lines

From: Waiman Long <[email protected]>
Sent: Monday, May 8, 2023 11:29 PM
To: Zhuo, Qiuxu <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Will Deacon <[email protected]>
Cc: Boqun Feng <[email protected]>; [email protected]
Subject: Re: [PATCH 1/1] locking/qspinlock: Fix state-transition changes in comments

Quoted 20 lines · Qiuxu Zhuo

1. There may be concurrent locker CPUs to set the qspinlock pending bit.

The first CPU (called pending CPU) of these CPUs sets the pending bit to make the state transition (the qspinlock pending bit is set):

0,0,* -> 0,1,*

The rest of these CPUs are queued to the MCS queue to make the state transition (the qspinlock tail is updated):

0,1,* -> *,1,*

The pending CPU waits until the locker owner goes away to make the state transition (the qspinlock locked field is set to zero):

*,1,* -> *,1,0

The pending CPU takes the ownership and clears the pending bit to make the state transition:

Quoted 6 lines

*,1,0 -> *,0,1

2. The header of the MCS queue takes the ownership and calls set_locked() to make the state transition:

*,*,0 -> *,*,1

That is not true. The pending bit owner has priority over the MCS queue head. So the pending bit must be 0 before the MCS queue head can take over the lock. So

Quoted 5 lines
Quoted 1 line

    *,0,0 -> *,0,1

Yes, the pending bit must be 0 before the header can take the lock.
But as the statement "There may be concurrent locker CPUs to set the qspinlock pending bit " at the beginning. So just after the header takes over the lock, there is also a possible concurrent locker CPU to set the pending bit. That means at this time point here, the pending bit could be either 0 or 1.

OK, I am looking from the point of view of the CPU doing the transition. Otherwise, for an arch with very weak memory ordering, anything is possible. i.e. *,*,* -> *,*,* if we have to consider what all the CPUs in the system can see.

Quoted 33 lines
Quoted 25 lines

kernel/locking/qspinlock.c static __always_inline u32

Quoted 13 lines

Fix the state-transition changes above in the code comments accordingly.

---
   kernel/locking/qspinlock.c | 10 ++++++----
   1 file changed, 6 insertions(+), 4 deletions(-)
kernel/locking/qspinlock.c +0 -0
index ebe6b8ec7cb3..efebbf19f887 100644
@@ -257,7 +257,7 @@ static __always_inline u32

queued_fetch_set_pending_acquire(struct qspinlock *lo

kernel/locking/qspinlock.c static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lo · line 260

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
    * set_locked - Set the lock bit and own the lock
    * @lock: Pointer to queued spinlock structure
    *
- * *,*,0 -> *,0,1
+ * *,*,0 -> *,*,1

set_locked() can only be called when it is sure that the pending bit isn't set.

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct

Quoted 4 lines
kernel/locking/qspinlock.c +0 -0
    */
   static __always_inline void set_locked(struct qspinlock *lock)
   {
@@ -348,7 +348,7 @@ void __lockfunc queued_spin_lock_slowpath(struct

qspinlock *lock, u32 val)

Quoted 8 lines

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 351

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
   	/*
   	 * trylock || pending
   	 *
-	 * 0,0,* -> 0,1,* -> 0,0,1 pending, trylock
+	 * 0,0,* -> 0,1,* -> ... -> *,0,1 pending, trylock

By the time trylock is done, there may be entries in the queue. However, I doubt it helps by adding "..." in between possible multiple transitions.

Quoted 3 lines

The added "..." means there could be entries in the queue before trylock done. This is just for making the state transitions more complete ;-). If you think it doesn't help, I can remove it in the next version.

The transition flow does not show whether there is entry in the queue or not. It just shows the state of the lock word.

Quoted 18 lines
Quoted 18 lines

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct

Quoted 5 lines
kernel/locking/qspinlock.c +0 -0
   	 */
   	val = queued_fetch_set_pending_acquire(lock);
 
@@ -358,6 +358,8 @@ void __lockfunc queued_spin_lock_slowpath(struct

qspinlock *lock, u32 val)

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 362

Quoted 5 lines
kernel/locking/qspinlock.c +2 -0
   	 * Undo and queue; our setting of PENDING might have made the
   	 * n,0,0 -> 0,0,0 transition fail and it will now be waiting
   	 * on @next to become !NULL.
+	 *
+	 * 0,1,* -> *,1,*

There is already a "n,0,0 -> 0,0,0" above, adding a new one may just confuse people.

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct

Quoted 4 lines
kernel/locking/qspinlock.c +0 -0
   	 */
   	if (unlikely(val & ~_Q_LOCKED_MASK)) {
 
@@ -371,7 +373,7 @@ void __lockfunc queued_spin_lock_slowpath(struct

qspinlock *lock, u32 val)

Quoted 8 lines
Quoted 6 lines

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 376

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
   	/*
   	 * We're pending, wait for the owner to go away.
   	 *
-	 * 0,1,1 -> *,1,0
+	 * *,1,* -> *,1,0

This refers to the wait loop. We don't need to wait if the owner has gone.

But just before we wait for the locked field, the locked field could be either 0 (the locker can release the lock at any time) or 1.

Again, I take the viewpoint of the CPU doing the wait. It will only wait if it observes that the lock isn't free.

Quoted 13 lines
Quoted 6 lines

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct

Quoted 5 lines
kernel/locking/qspinlock.c +0 -0
   	 *
   	 * this wait loop must be a load-acquire such that we match the
   	 * store-release that clears the locked bit and create lock
@@ -385,7 +387,7 @@ void __lockfunc queued_spin_lock_slowpath(struct

qspinlock *lock, u32 val)

Quoted 7 lines

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 390

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
   	/*
   	 * take ownership and clear the pending bit.
   	 *
-	 * 0,1,0 -> 0,0,1
+	 * *,1,0 -> *,0,1

That is the part that we can make the change in the transition diagram as noted.

Quoted 3 lines

Sorry, I'm not clear about your request.
Did you mean just make the change "*,1,0 -> *,0,1" above in the transition diagram or all the changes above in the transition diagram?

What I meant is that this particular change is correct AFAICS. Sorry for the confusion.

Cheers,
Longman

#4 lore raw

Hi Longman,

OK, so, you are from the point of view of the CPU (the lock word value it read last time) doing state transition in the uncontended scenario:

     [1]:  Old lock word value (this CPU read it last time) ---> New lock word value

I'm from the point of view of the current lock word possible value that its state is transited to the new lock word possible value (I don't think I'm the only one from this point of view when reading the qspinlock code ;-).

     [2]: Current lock word possible value ---> New lock word possible value 

I'm fine to keep the view of [1] in the current code and get [2] as the backup. I'll send out a v2 with just two minor comments' fixes.

Thanks!
-Qiuxu

Quoted 172 lines

From: Waiman Long <[email protected]>
Sent: Tuesday, May 9, 2023 10:31 AM
To: Zhuo, Qiuxu <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Will Deacon <[email protected]>
Cc: Boqun Feng <[email protected]>; [email protected]
Subject: Re: [PATCH 1/1] locking/qspinlock: Fix state-transition changes in comments

Quoted 23 lines · Qiuxu

Hi Wainman,

Thanks for your review. Please see the comments below.

Quoted 19 lines

From: Waiman Long <[email protected]>
Sent: Monday, May 8, 2023 11:29 PM
To: Zhuo, Qiuxu <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Will Deacon <[email protected]>
Cc: Boqun Feng <[email protected]>; [email protected]
Subject: Re: [PATCH 1/1] locking/qspinlock: Fix state-transition changes in comments

Quoted 8 lines · Qiuxu Zhuo

1. There may be concurrent locker CPUs to set the qspinlock pending bit.

The first CPU (called pending CPU) of these CPUs sets the pending bit to make the state transition (the qspinlock pending bit is set):

0,0,* -> 0,1,*

The rest of these CPUs are queued to the MCS queue to make the

state

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

transition (the qspinlock tail is updated):

0,1,* -> *,1,*

The pending CPU waits until the locker owner goes away to make the state transition (the qspinlock locked field is set to zero):

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

*,1,* -> *,1,0

The pending CPU takes the ownership and clears the pending bit to make the state transition:

*,1,0 -> *,0,1

2. The header of the MCS queue takes the ownership and calls

set_locked()

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

to make the state transition:

*,*,0 -> *,*,1

That is not true. The pending bit owner has priority over the MCS queue head. So the pending bit must be 0 before the MCS queue head can take over the lock. So

Quoted 5 lines
Quoted 1 line

    *,0,0 -> *,0,1

Yes, the pending bit must be 0 before the header can take the lock. But as the statement "There may be concurrent locker CPUs to set the qspinlock pending bit " at the beginning. So just after the header takes over the lock, there is also a possible concurrent locker CPU to set the

pending bit. That means at this time point here, the pending bit could be either 0 or 1.

OK, I am looking from the point of view of the CPU doing the transition. Otherwise, for an arch with very weak memory ordering, anything is possible. i.e. *,*,* -> *,*,* if we have to consider what all the CPUs in the system can see.

Quoted 19 lines
Quoted 19 lines

kernel/locking/qspinlock.c static __always_inline u32

Quoted 13 lines

Fix the state-transition changes above in the code comments accordingly.

---
   kernel/locking/qspinlock.c | 10 ++++++----
   1 file changed, 6 insertions(+), 4 deletions(-)
kernel/locking/qspinlock.c +0 -0
index ebe6b8ec7cb3..efebbf19f887 100644
@@ -257,7 +257,7 @@ static __always_inline u32

queued_fetch_set_pending_acquire(struct qspinlock *lo

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
    * set_locked - Set the lock bit and own the lock
    * @lock: Pointer to queued spinlock structure
    *
- * *,*,0 -> *,0,1
+ * *,*,0 -> *,*,1
Quoted 5 lines
Quoted 5 lines

set_locked() can only be called when it is sure that the pending bit isn't set.

kernel/locking/qspinlock.c void __lockfunc

Quoted 4 lines
kernel/locking/qspinlock.c +0 -0
    */
   static __always_inline void set_locked(struct qspinlock *lock)
   {
@@ -348,7 +348,7 @@ void __lockfunc

queued_spin_lock_slowpath(struct

Quoted 8 lines
Quoted 8 lines

qspinlock *lock, u32 val)

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 351

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
   	/*
   	 * trylock || pending
   	 *
-	 * 0,0,* -> 0,1,* -> 0,0,1 pending, trylock
+	 * 0,0,* -> 0,1,* -> ... -> *,0,1 pending, trylock

By the time trylock is done, there may be entries in the queue. However, I doubt it helps by adding "..." in between possible multiple

transitions.

Quoted 2 lines
Quoted 1 line

The added "..." means there could be entries in the queue before trylock

done.

Quoted 2 lines

This is just for making the state transitions more complete ;-). If you think it doesn't help, I can remove it in the next version.

The transition flow does not show whether there is entry in the queue or not. It just shows the state of the lock word.

Quoted 5 lines
Quoted 5 lines

kernel/locking/qspinlock.c void __lockfunc

Quoted 5 lines
kernel/locking/qspinlock.c +0 -0
   	 */
   	val = queued_fetch_set_pending_acquire(lock);
 
@@ -358,6 +358,8 @@ void __lockfunc

queued_spin_lock_slowpath(struct

Quoted 8 lines
Quoted 8 lines

qspinlock *lock, u32 val)

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 362

Quoted 5 lines
kernel/locking/qspinlock.c +2 -0
   	 * Undo and queue; our setting of PENDING might have made the
   	 * n,0,0 -> 0,0,0 transition fail and it will now be waiting
   	 * on @next to become !NULL.
+	 *
+	 * 0,1,* -> *,1,*

There is already a "n,0,0 -> 0,0,0" above, adding a new one may just confuse people.

Quoted 4 lines
Quoted 4 lines

kernel/locking/qspinlock.c void __lockfunc

Quoted 4 lines
kernel/locking/qspinlock.c +0 -0
   	 */
   	if (unlikely(val & ~_Q_LOCKED_MASK)) {
 
@@ -371,7 +373,7 @@ void __lockfunc

queued_spin_lock_slowpath(struct

Quoted 9 lines
Quoted 7 lines

qspinlock *lock, u32 val)

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 376

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
   	/*
   	 * We're pending, wait for the owner to go away.
   	 *
-	 * 0,1,1 -> *,1,0
+	 * *,1,* -> *,1,0

This refers to the wait loop. We don't need to wait if the owner has gone.

But just before we wait for the locked field, the locked field could be either 0 (the locker can release the lock at any time) or 1.

Again, I take the viewpoint of the CPU doing the wait. It will only wait if it observes that the lock isn't free.

Quoted 5 lines
Quoted 5 lines

kernel/locking/qspinlock.c void __lockfunc

Quoted 5 lines
kernel/locking/qspinlock.c +0 -0
   	 *
   	 * this wait loop must be a load-acquire such that we match the
   	 * store-release that clears the locked bit and create lock
@@ -385,7 +387,7 @@ void __lockfunc

queued_spin_lock_slowpath(struct

Quoted 10 lines
Quoted 8 lines

qspinlock *lock, u32 val)

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 390

Quoted 5 lines
kernel/locking/qspinlock.c +1 -1
   	/*
   	 * take ownership and clear the pending bit.
   	 *
-	 * 0,1,0 -> 0,0,1
+	 * *,1,0 -> *,0,1

That is the part that we can make the change in the transition diagram as noted.

Sorry, I'm not clear about your request.
Did you mean just make the change "*,1,0 -> *,0,1" above in the transition

diagram or

Quoted 1 line

all the changes above in the transition diagram?

What I meant is that this particular change is correct AFAICS. Sorry for the confusion.

Cheers,
Longman

author
#5 lore raw

PATCH v2 1/1 locking/qspinlock: Fix state-transition changes in comments

1. set_locked() only sets the locked field to 1 and doesn't touch the pending field. So the correct lock state transition is:

         *,*,0 -> *,*,1

2. The initial lock state when calling clear_pending_set_locked() is the state just after waiting for the locker goes away. So the state transition for clear_pending_set_locked() is:

         *,1,0 -> *,0,1

---
v1->v2:
- Drop the state transition comments fixes (viewed from the current lock word value).
- Update the commit message accordingly.

 kernel/locking/qspinlock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/locking/qspinlock.c +2 -2
index ebe6b8ec7cb3..256021c87ac1 100644
@@ -257,7 +257,7 @@ static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lo
  * set_locked - Set the lock bit and own the lock
  * @lock: Pointer to queued spinlock structure
  *
- * *,*,0 -> *,0,1
+ * *,*,0 -> *,*,1
  */
 static __always_inline void set_locked(struct qspinlock *lock)
 {
@@ -385,7 +385,7 @@ void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
 	/*
 	 * take ownership and clear the pending bit.
 	 *
-	 * 0,1,0 -> 0,0,1
+	 * *,1,0 -> *,0,1
 	 */
 	clear_pending_set_locked(lock);
 	lockevent_inc(lock_pending);
Signature · 1 line
2.17.1
reviewer Acked-by on v2 1/1
#6 lore raw
Quoted 34 lines · Qiuxu Zhuo

1. set_locked() only sets the locked field to 1 and doesn't touch the pending field. So the correct lock state transition is:

*,*,0 -> *,*,1

2. The initial lock state when calling clear_pending_set_locked() is the state just after waiting for the locker goes away. So the state transition for clear_pending_set_locked() is:

*,1,0 -> *,0,1

---
v1->v2:
- Drop the state transition comments fixes (viewed from the current lock word value).
- Update the commit message accordingly.

  kernel/locking/qspinlock.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
kernel/locking/qspinlock.c +1 -1
index ebe6b8ec7cb3..256021c87ac1 100644
@@ -257,7 +257,7 @@ static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lo
   * set_locked - Set the lock bit and own the lock
   * @lock: Pointer to queued spinlock structure
   *
- * *,*,0 -> *,0,1
+ * *,*,0 -> *,*,1
   */
  static __always_inline void set_locked(struct qspinlock *lock)
  {

kernel/locking/qspinlock.c void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) · line 391

Quoted 8 lines
kernel/locking/qspinlock.c +1 -1
@@ -385,7 +385,7 @@ void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
  	/*
  	 * take ownership and clear the pending bit.
  	 *
-	 * 0,1,0 -> 0,0,1
+	 * *,1,0 -> *,0,1
  	 */
  	clear_pending_set_locked(lock);
  	lockevent_inc(lock_pending);
reviewer
#7 lore raw
In reply to Zhuo, Qiuxu
Quoted 7 lines · Qiuxu

Hi Longman,

OK, so, you are from the point of view of the CPU (the lock word value it read last time) doing state transition in the uncontended scenario:

      [1]:  Old lock word value (this CPU read it last time) ---> New lock word value
Quoted 8 lines

I'm from the point of view of the current lock word possible value that its state is transited to the new lock word possible value (I don't think I'm the only one from this point of view when reading the qspinlock code ;-).

      [2]: Current lock word possible value ---> New lock word possible value

I'm fine to keep the view of [1] in the current code and get [2] as the backup. I'll send out a v2 with just two minor comments' fixes.

The purpose of those transition flow charts is to help understand what the code is doing. So they should reflect the running CPU point of view. If you want to show what all the possible values can be, you have to explicitly state that to avoid the confusion.

Thanks,
Longman

#8 lore raw
Quoted 17 lines

From: Waiman Long <[email protected]>
...

Quoted 10 lines

OK, so, you are from the point of view of the CPU (the lock word value it read last time) doing state transition in the uncontended scenario:

      [1]:  Old lock word value (this CPU read it last time) ---> New

lock word value

I'm from the point of view of the current lock word possible value that its state is transited to the new lock word possible value (I don't think I'm the only one from this point of view when reading the

qspinlock code ;-).

Quoted 4 lines
      [2]: Current lock word possible value ---> New lock word

possible value

Quoted 8 lines
Quoted 1 line

I'm fine to keep the view of [1] in the current code and get [2] as the

backup.

Quoted 1 line

I'll send out a v2 with just two minor comments' fixes.

The purpose of those transition flow charts is to help understand what the code is doing. So they should reflect the running CPU point of view. If you want to show what all the possible values can be, you have to explicitly state that to avoid the confusion.

Yes, from the uncontended case view can help readers understand the qspinlock code. After that, readers find that the qspinlock code also works for contended cases.

Thank you for the clarification of the purpose of those state transition comments.

-Qiuxu

Quoted 2 lines

Thanks,
Longman