Skip to contentSkip to conversation

locking/osq_lock: annotate a data race in osq_lock

PATCH In review Qian Cai 3 participants 16 messages Feb 11, 2020 – May 11, 2020 Locking primitives

lore
Share & tools
lore

Qian Cai

author
#0 lore raw

PATCH v2 1/1 locking/osq_lock: annotate a data race in osq_lock

prev->next could be accessed concurrently as noticed by KCSAN,

write (marked) to 0xffff9d3370dbbe40 of 8 bytes by task 3294 on cpu 107:

  osq_lock+0x25f/0x350
  osq_wait_next at kernel/locking/osq_lock.c:79
  (inlined by) osq_lock at kernel/locking/osq_lock.c:185
  rwsem_optimistic_spin
  <snip>

read to 0xffff9d3370dbbe40 of 8 bytes by task 3398 on cpu 100:

  osq_lock+0x196/0x350
  osq_lock at kernel/locking/osq_lock.c:157
  rwsem_optimistic_spin
  <snip>

Since the write only stores NULL to prev->next and the read tests if prev->next equals to this_cpu_ptr(&osq_node). Even if the value is shattered, the code is still working correctly. Thus, mark it as an intentional data race using the data_race() macro.

---

v2: insert some code comments.

 kernel/locking/osq_lock.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/locking/osq_lock.c +5 -1
index 1f7734949ac8..f733bcd99e8a 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next == node) &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;
 
Signature · 1 line
1.8.3.1
author
#1 lore raw
Quoted 19 lines · Qian Cai

prev->next could be accessed concurrently as noticed by KCSAN,

write (marked) to 0xffff9d3370dbbe40 of 8 bytes by task 3294 on cpu 107:

 osq_lock+0x25f/0x350
 osq_wait_next at kernel/locking/osq_lock.c:79
 (inlined by) osq_lock at kernel/locking/osq_lock.c:185
 rwsem_optimistic_spin
 <snip>

read to 0xffff9d3370dbbe40 of 8 bytes by task 3398 on cpu 100:

 osq_lock+0x196/0x350
 osq_lock at kernel/locking/osq_lock.c:157
 rwsem_optimistic_spin
 <snip>
Quoted 6 lines

Since the write only stores NULL to prev->next and the read tests if prev->next equals to this_cpu_ptr(&osq_node). Even if the value is shattered, the code is still working correctly. Thus, mark it as an intentional data race using the data_race() macro.

Hmm, this patch has been dropped from linux-next from some reasons.

Paul, can you pick this up along with KCSAN fixes?

https://lore.kernel.org/lkml/[email protected]/

Quoted 27 lines

---

v2: insert some code comments.

kernel/locking/osq_lock.c | 6 +++++-

1 file changed, 5 insertions(+), 1 deletion(-)
kernel/locking/osq_lock.c +5 -1
index 1f7734949ac8..f733bcd99e8a 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next == node) &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;
 
Signature · 1 line
1.8.3.1
#2 lore raw
Quoted 24 lines · Qian Cai
Quoted 24 lines · Qian Cai

prev->next could be accessed concurrently as noticed by KCSAN,

write (marked) to 0xffff9d3370dbbe40 of 8 bytes by task 3294 on cpu 107:

 osq_lock+0x25f/0x350
 osq_wait_next at kernel/locking/osq_lock.c:79
 (inlined by) osq_lock at kernel/locking/osq_lock.c:185
 rwsem_optimistic_spin
 <snip>

read to 0xffff9d3370dbbe40 of 8 bytes by task 3398 on cpu 100:

 osq_lock+0x196/0x350
 osq_lock at kernel/locking/osq_lock.c:157
 rwsem_optimistic_spin
 <snip>

Since the write only stores NULL to prev->next and the read tests if prev->next equals to this_cpu_ptr(&osq_node). Even if the value is shattered, the code is still working correctly. Thus, mark it as an intentional data race using the data_race() macro.

Quoted 7 lines
Quoted 1 line

Hmm, this patch has been dropped from linux-next from some reasons.

Paul, can you pick this up along with KCSAN fixes?

https://lore.kernel.org/lkml/[email protected]/

I have queued it on -rcu, but it is too late for v5.8 via the -rcu tree, so this is v5.9 at the earliest. Plus I would need an ack from one of the locking folks.

Thanx, Paul

Quoted 28 lines
Quoted 27 lines

---

v2: insert some code comments.

kernel/locking/osq_lock.c | 6 +++++-

1 file changed, 5 insertions(+), 1 deletion(-)
kernel/locking/osq_lock.c +5 -1
index 1f7734949ac8..f733bcd99e8a 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next == node) &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;
 
Signature · 1 line
1.8.3.1
author
#3 lore raw
Quoted 33 lines · Paul E. McKenney
Quoted 28 lines · Qian Cai
Quoted 19 lines · Qian Cai

prev->next could be accessed concurrently as noticed by KCSAN,

write (marked) to 0xffff9d3370dbbe40 of 8 bytes by task 3294 on cpu 107:
osq_lock+0x25f/0x350
osq_wait_next at kernel/locking/osq_lock.c:79
(inlined by) osq_lock at kernel/locking/osq_lock.c:185 rwsem_optimistic_spin
<snip>

read to 0xffff9d3370dbbe40 of 8 bytes by task 3398 on cpu 100:
osq_lock+0x196/0x350
osq_lock at kernel/locking/osq_lock.c:157 rwsem_optimistic_spin
<snip>

Quoted 6 lines

Since the write only stores NULL to prev->next and the read tests if prev->next equals to this_cpu_ptr(&osq_node). Even if the value is shattered, the code is still working correctly. Thus, mark it as an intentional data race using the data_race() macro.

Hmm, this patch has been dropped from linux-next from some reasons.

Quoted 7 lines
Quoted 3 lines

Paul, can you pick this up along with KCSAN fixes?

https://lore.kernel.org/lkml/[email protected]/

I have queued it on -rcu, but it is too late for v5.8 via the -rcu tree, so this is v5.9 at the earliest. Plus I would need an ack from one of the locking folks.

Peter, Will, can you give an ACK? This v2 should incorporate all the feedback from Peter,

https://lore.kernel.org/lkml/[email protected]/

V5.9 is fine. All I care about is it is always in linux-next (so the testing bots won’t trigger this over and over again) and to be in mainline at some point in the future.

Quoted 29 lines

Thanx, Paul

Quoted 26 lines
Quoted 26 lines

---

v2: insert some code comments.

kernel/locking/osq_lock.c | 6 +++++-

1 file changed, 5 insertions(+), 1 deletion(-)
kernel/locking/osq_lock.c +5 -1
index 1f7734949ac8..f733bcd99e8a 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next == node) &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;
 
Signature · 1 line
1.8.3.1
#4 lore raw
Quoted 41 lines · Qian Cai
Quoted 33 lines · Paul E. McKenney
Quoted 28 lines · Qian Cai
Quoted 19 lines · Qian Cai

prev->next could be accessed concurrently as noticed by KCSAN,

write (marked) to 0xffff9d3370dbbe40 of 8 bytes by task 3294 on cpu 107:
osq_lock+0x25f/0x350
osq_wait_next at kernel/locking/osq_lock.c:79
(inlined by) osq_lock at kernel/locking/osq_lock.c:185 rwsem_optimistic_spin
<snip>

read to 0xffff9d3370dbbe40 of 8 bytes by task 3398 on cpu 100:
osq_lock+0x196/0x350
osq_lock at kernel/locking/osq_lock.c:157 rwsem_optimistic_spin
<snip>

Quoted 6 lines

Since the write only stores NULL to prev->next and the read tests if prev->next equals to this_cpu_ptr(&osq_node). Even if the value is shattered, the code is still working correctly. Thus, mark it as an intentional data race using the data_race() macro.

Hmm, this patch has been dropped from linux-next from some reasons.

Quoted 7 lines
Quoted 3 lines

Paul, can you pick this up along with KCSAN fixes?

https://lore.kernel.org/lkml/[email protected]/

I have queued it on -rcu, but it is too late for v5.8 via the -rcu tree, so this is v5.9 at the earliest. Plus I would need an ack from one of the locking folks.

Quoted 5 lines

Peter, Will, can you give an ACK? This v2 should incorporate all the feedback from Peter,

https://lore.kernel.org/lkml/[email protected]/

V5.9 is fine. All I care about is it is always in linux-next (so the testing bots won’t trigger this over and over again) and to be in mainline at some point in the future.

Ah, and I forgot to ask. Why "if (data_race(prev->next == node)" instead of "if (data_race(prev->next) == node"?

Thanx, Paul

Quoted 27 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines

---

v2: insert some code comments.

kernel/locking/osq_lock.c | 6 +++++-

1 file changed, 5 insertions(+), 1 deletion(-)
kernel/locking/osq_lock.c +5 -1
index 1f7734949ac8..f733bcd99e8a 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next == node) &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;
 
Signature · 1 line
1.8.3.1
author
#5 lore raw
Quoted 6 lines · Paul E. McKenney

Ah, and I forgot to ask. Why "if (data_race(prev->next == node)" instead of "if (data_race(prev->next) == node"?

I think the one you suggested is slightly better to point out the exact race. Do you want me to resend or you could squash it instead?

#6 lore raw
Quoted 8 lines · Qian Cai
Quoted 6 lines · Paul E. McKenney

Ah, and I forgot to ask. Why "if (data_race(prev->next == node)" instead of "if (data_race(prev->next) == node"?

I think the one you suggested is slightly better to point out the exact race. Do you want me to resend or you could squash it instead?

The patch was still at the top of my stack, so I just amended it. Here is the updated version.

Thanx, Paul

------------------------------------------------------------------------

commit 13e69ca01ce1621ce74248bda86cfad47fa5a0fa

Author: Qian Cai <[email protected]>
Date:   Tue Feb 11 08:54:15 2020 -0500
    locking/osq_lock: Annotate a data race in osq_lock
    
    The prev->next pointer can be accessed concurrently as noticed by KCSAN:
    
     write (marked) to 0xffff9d3370dbbe40 of 8 bytes by task 3294 on cpu 107:
      osq_lock+0x25f/0x350
      osq_wait_next at kernel/locking/osq_lock.c:79
      (inlined by) osq_lock at kernel/locking/osq_lock.c:185
      rwsem_optimistic_spin
      <snip>
    
     read to 0xffff9d3370dbbe40 of 8 bytes by task 3398 on cpu 100:
      osq_lock+0x196/0x350
      osq_lock at kernel/locking/osq_lock.c:157
      rwsem_optimistic_spin
      <snip>
    
    Since the write only stores NULL to prev->next and the read tests if
    prev->next equals to this_cpu_ptr(&osq_node). Even if the value is
    shattered, the code is still working correctly. Thus, mark it as an
    intentional data race using the data_race() macro.
    
    Signed-off-by: Qian Cai <[email protected]>
    Signed-off-by: Paul E. McKenney <[email protected]>
kernel/locking/osq_lock.c +5 -1
index 1f77349..1de006e 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next) == node &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;
 
maintainer
#7 lore raw
Quoted 52 lines · Paul E. McKenney
Quoted 8 lines · Qian Cai
Quoted 6 lines · Paul E. McKenney

Ah, and I forgot to ask. Why "if (data_race(prev->next == node)" instead of "if (data_race(prev->next) == node"?

I think the one you suggested is slightly better to point out the exact race. Do you want me to resend or you could squash it instead?

The patch was still at the top of my stack, so I just amended it. Here is the updated version.

Thanx, Paul

------------------------------------------------------------------------

commit 13e69ca01ce1621ce74248bda86cfad47fa5a0fa

Author: Qian Cai <[email protected]>
Date:   Tue Feb 11 08:54:15 2020 -0500
    locking/osq_lock: Annotate a data race in osq_lock
    
    The prev->next pointer can be accessed concurrently as noticed by KCSAN:
    
     write (marked) to 0xffff9d3370dbbe40 of 8 bytes by task 3294 on cpu 107:
      osq_lock+0x25f/0x350
      osq_wait_next at kernel/locking/osq_lock.c:79
      (inlined by) osq_lock at kernel/locking/osq_lock.c:185
      rwsem_optimistic_spin
      <snip>
    
     read to 0xffff9d3370dbbe40 of 8 bytes by task 3398 on cpu 100:
      osq_lock+0x196/0x350
      osq_lock at kernel/locking/osq_lock.c:157
      rwsem_optimistic_spin
      <snip>
    
    Since the write only stores NULL to prev->next and the read tests if
    prev->next equals to this_cpu_ptr(&osq_node). Even if the value is
    shattered, the code is still working correctly. Thus, mark it as an
    intentional data race using the data_race() macro.
    
    Signed-off-by: Qian Cai <[email protected]>
    Signed-off-by: Paul E. McKenney <[email protected]>
kernel/locking/osq_lock.c +0 -0
index 1f77349..1de006e 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */

kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 163

Quoted 10 lines
kernel/locking/osq_lock.c +5 -1
 
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next) == node &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;

I'm fine with the data_race() placement, but I don't find the comment very helpful. We assign the result of a READ_ONCE() to 'prev' in the loop, so I don't think that the cpu_relax() is really relevant.

The reason we don't need READ_ONCE() here is because if we race with the writer then either we'll go round the loop again after accidentally thinking prev->next != node, or we'll erroneously attempt the cmpxchg() because we thought they were equal and that will fail.

Make sense?

Will

#8 lore raw
Quoted 54 lines · Will Deacon
Quoted 53 lines · Paul E. McKenney
Quoted 8 lines · Qian Cai
Quoted 6 lines · Paul E. McKenney

Ah, and I forgot to ask. Why "if (data_race(prev->next == node)" instead of "if (data_race(prev->next) == node"?

I think the one you suggested is slightly better to point out the exact race. Do you want me to resend or you could squash it instead?

The patch was still at the top of my stack, so I just amended it. Here is the updated version.

Thanx, Paul

------------------------------------------------------------------------

commit 13e69ca01ce1621ce74248bda86cfad47fa5a0fa

Author: Qian Cai <[email protected]>
Date:   Tue Feb 11 08:54:15 2020 -0500
    locking/osq_lock: Annotate a data race in osq_lock
    
    The prev->next pointer can be accessed concurrently as noticed by KCSAN:
    
     write (marked) to 0xffff9d3370dbbe40 of 8 bytes by task 3294 on cpu 107:
      osq_lock+0x25f/0x350
      osq_wait_next at kernel/locking/osq_lock.c:79
      (inlined by) osq_lock at kernel/locking/osq_lock.c:185
      rwsem_optimistic_spin
      <snip>
    
     read to 0xffff9d3370dbbe40 of 8 bytes by task 3398 on cpu 100:
      osq_lock+0x196/0x350
      osq_lock at kernel/locking/osq_lock.c:157
      rwsem_optimistic_spin
      <snip>
    
    Since the write only stores NULL to prev->next and the read tests if
    prev->next equals to this_cpu_ptr(&osq_node). Even if the value is
    shattered, the code is still working correctly. Thus, mark it as an
    intentional data race using the data_race() macro.
    
    Signed-off-by: Qian Cai <[email protected]>
    Signed-off-by: Paul E. McKenney <[email protected]>
kernel/locking/osq_lock.c +0 -0
index 1f77349..1de006e 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
Quoted 13 lines

kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 163

Quoted 9 lines
kernel/locking/osq_lock.c +5 -1
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next) == node &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;

I'm fine with the data_race() placement, but I don't find the comment very helpful. We assign the result of a READ_ONCE() to 'prev' in the loop, so I don't think that the cpu_relax() is really relevant.

Suppose that the compiler loaded a value that was not equal to "node". In that case, the cmpxchg() won't happen, so something else must force the compiler to do the reload in order to avoid an infinite loop, right? Or am I missing something here?

Thanx, Paul

Quoted 8 lines

The reason we don't need READ_ONCE() here is because if we race with the writer then either we'll go round the loop again after accidentally thinking prev->next != node, or we'll erroneously attempt the cmpxchg() because we thought they were equal and that will fail.

Make sense?

Will

maintainer
#9 lore raw
Quoted 23 lines · Paul E. McKenney
Quoted 8 lines · Will Deacon
Quoted 7 lines · Paul E. McKenney
kernel/locking/osq_lock.c +0 -0
index 1f77349..1de006e 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
Quoted 13 lines

kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 163

Quoted 9 lines
kernel/locking/osq_lock.c +5 -1
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next) == node &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;

I'm fine with the data_race() placement, but I don't find the comment very helpful. We assign the result of a READ_ONCE() to 'prev' in the loop, so I don't think that the cpu_relax() is really relevant.

Quoted 4 lines

Suppose that the compiler loaded a value that was not equal to "node". In that case, the cmpxchg() won't happen, so something else must force the compiler to do the reload in order to avoid an infinite loop, right? Or am I missing something here?

Then we just go round the loop and reload prev:

	prev = READ_ONCE(node->prev);

which should be enough to stop the compiler, no?

Will

#10 lore raw
Quoted 29 lines · Will Deacon
Quoted 23 lines · Paul E. McKenney
Quoted 8 lines · Will Deacon
Quoted 7 lines · Paul E. McKenney
kernel/locking/osq_lock.c +0 -0
index 1f77349..1de006e 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
Quoted 13 lines

kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 163

Quoted 9 lines
kernel/locking/osq_lock.c +5 -1
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next) == node &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;

I'm fine with the data_race() placement, but I don't find the comment very helpful. We assign the result of a READ_ONCE() to 'prev' in the loop, so I don't think that the cpu_relax() is really relevant.

Quoted 4 lines

Suppose that the compiler loaded a value that was not equal to "node". In that case, the cmpxchg() won't happen, so something else must force the compiler to do the reload in order to avoid an infinite loop, right? Or am I missing something here?

Quoted 5 lines

Then we just go round the loop and reload prev:

	prev = READ_ONCE(node->prev);

which should be enough to stop the compiler, no?

Yes, that would also work. Either have the cpu_relax() or a barrier() or whatever on the one hand, or, as you say, turn the data_race() into a READ_ONCE(). I personally prefer the READ_ONCE() myself, unless that would undesirably suppress other KCSAN warnings.

Thanx, Paul

maintainer
#11 lore raw
Quoted 32 lines · Paul E. McKenney
Quoted 31 lines · Will Deacon
Quoted 23 lines · Paul E. McKenney
Quoted 8 lines · Will Deacon
Quoted 7 lines · Paul E. McKenney
kernel/locking/osq_lock.c +0 -0
index 1f77349..1de006e 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
Quoted 13 lines

kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 163

Quoted 9 lines
kernel/locking/osq_lock.c +5 -1
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next) == node &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;

I'm fine with the data_race() placement, but I don't find the comment very helpful. We assign the result of a READ_ONCE() to 'prev' in the loop, so I don't think that the cpu_relax() is really relevant.

Quoted 4 lines

Suppose that the compiler loaded a value that was not equal to "node". In that case, the cmpxchg() won't happen, so something else must force the compiler to do the reload in order to avoid an infinite loop, right? Or am I missing something here?

Then we just go round the loop and reload prev:

Quoted 8 lines
Quoted 3 lines
	prev = READ_ONCE(node->prev);

which should be enough to stop the compiler, no?

Yes, that would also work. Either have the cpu_relax() or a barrier() or whatever on the one hand, or, as you say, turn the data_race() into a READ_ONCE(). I personally prefer the READ_ONCE() myself, unless that would undesirably suppress other KCSAN warnings.

No, I mean here is the code after this patch is applied:

	for (;;) {
		if (data_race(prev->next) == node &&
		    cmpxchg(&prev->next, node, NULL) == node)
			break;

		/*
		 * We can only fail the cmpxchg() racing against an unlock(),
		 * in which case we should observe @node->locked becomming
		 * true.
		 */
		if (smp_load_acquire(&node->locked))
			return true;

		cpu_relax();

		/*
		 * Or we race against a concurrent unqueue()'s step-B, in which
		 * case its step-C will write us a new @node->prev pointer.
		 */
		prev = READ_ONCE(node->prev);
	}

I'm saying that this READ_ONCE at the end of the loop should be sufficient to stop the compiler making value assumptions about prev->next. Do you agree?

Will

#12 lore raw
Quoted 66 lines · Will Deacon
Quoted 32 lines · Paul E. McKenney
Quoted 31 lines · Will Deacon
Quoted 23 lines · Paul E. McKenney
Quoted 8 lines · Will Deacon
Quoted 7 lines · Paul E. McKenney
kernel/locking/osq_lock.c +0 -0
index 1f77349..1de006e 100644
@@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 */
 
Quoted 13 lines

kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 163

Quoted 9 lines
kernel/locking/osq_lock.c +5 -1
 	for (;;) {
-		if (prev->next == node &&
+		/*
+		 * cpu_relax() below implies a compiler barrier which would
+		 * prevent this comparison being optimized away.
+		 */
+		if (data_race(prev->next) == node &&
 		    cmpxchg(&prev->next, node, NULL) == node)
 			break;

I'm fine with the data_race() placement, but I don't find the comment very helpful. We assign the result of a READ_ONCE() to 'prev' in the loop, so I don't think that the cpu_relax() is really relevant.

Quoted 4 lines

Suppose that the compiler loaded a value that was not equal to "node". In that case, the cmpxchg() won't happen, so something else must force the compiler to do the reload in order to avoid an infinite loop, right? Or am I missing something here?

Then we just go round the loop and reload prev:

Quoted 8 lines
Quoted 3 lines
	prev = READ_ONCE(node->prev);

which should be enough to stop the compiler, no?

Yes, that would also work. Either have the cpu_relax() or a barrier() or whatever on the one hand, or, as you say, turn the data_race() into a READ_ONCE(). I personally prefer the READ_ONCE() myself, unless that would undesirably suppress other KCSAN warnings.

No, I mean here is the code after this patch is applied:

	for (;;) {
		if (data_race(prev->next) == node &&
		    cmpxchg(&prev->next, node, NULL) == node)
			break;

		/*
		 * We can only fail the cmpxchg() racing against an unlock(),
		 * in which case we should observe @node->locked becomming
		 * true.
		 */
		if (smp_load_acquire(&node->locked))
			return true;

		cpu_relax();

		/*
		 * Or we race against a concurrent unqueue()'s step-B, in which
		 * case its step-C will write us a new @node->prev pointer.
		 */
		prev = READ_ONCE(node->prev);
	}
Quoted 3 lines

I'm saying that this READ_ONCE at the end of the loop should be sufficient to stop the compiler making value assumptions about prev->next. Do you agree?

Good point, and I would certainly hope so!

Thanx, Paul

author
#13 lore raw
In reply to Will Deacon
Quoted 8 lines · Will Deacon

I'm fine with the data_race() placement, but I don't find the comment very helpful. We assign the result of a READ_ONCE() to 'prev' in the loop, so I don't think that the cpu_relax() is really relevant.

Quoted 6 lines

The reason we don't need READ_ONCE() here is because if we race with the writer then either we'll go round the loop again after accidentally thinking prev->next != node, or we'll erroneously attempt the cmpxchg() because we thought they were equal and that will fail.

Make sense?

I think the significant concern from the previous reviews was if compilers could prove that prev->next == node was always true because it had no knowledge of the concurrency, and then took out the whole if statement away resulting in an infinite loop.

The comment tried to explain that the cpu_relax() would save us from the infinite loop in theory here.

maintainer
#14 lore raw
Quoted 13 lines · Qian Cai
Quoted 13 lines · Will Deacon

I'm fine with the data_race() placement, but I don't find the comment very helpful. We assign the result of a READ_ONCE() to 'prev' in the loop, so I don't think that the cpu_relax() is really relevant.

The reason we don't need READ_ONCE() here is because if we race with the writer then either we'll go round the loop again after accidentally thinking prev->next != node, or we'll erroneously attempt the cmpxchg() because we thought they were equal and that will fail.

Quoted 6 lines
Quoted 1 line

Make sense?

I think the significant concern from the previous reviews was if compilers could prove that prev->next == node was always true because it had no knowledge of the concurrency, and then took out the whole if statement away resulting in an infinite loop.

Hmm, I don't see how it can remove the cmpxchg(). Do you have a link to that discussion, please?

Will

author
#15 lore raw
Quoted 6 lines · Will Deacon

Hmm, I don't see how it can remove the cmpxchg(). Do you have a link to that discussion, please?

lore.kernel.org/lkml/[email protected]

Correction — if compilers could prove ”prev->next != node” is always true, that cmpxchg() would not run. cpu_relax() should be sufficient to keep that “if statement” been optimized away in any case.