Skip to contentSkip to conversation

lib/xe/spin: Use READ_ONCE, WRITE_ONCE macros in Xe spin library

PATCH In review Matthew Brost 4 participants 5 messages Apr 16, 2025 – Apr 17, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/xe/spin: Use READ_ONCE, WRITE_ONCE macros in Xe spin library

Without the READ_ONCE macro is xe_spin_started(), a compiler may choose to read the value of spin->start exactly once rather than every call of xe_spin_started(). If the initial read is before the GPU batch execution flips spin->start to a non-zero value, the test will hang forever. This was shown on Chrome.

For uniformity, update xe_spin_end to use WRITE_ONCE too.

---
 lib/xe/xe_spin.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/xe/xe_spin.c +2 -2
index 0de0b1f2e9..65c29284aa 100644
@@ -145,7 +145,7 @@ void xe_spin_init(struct xe_spin *spin, struct xe_spin_opts *opts)
  */
 bool xe_spin_started(struct xe_spin *spin)
 {
-	return spin->start != 0;
+	return READ_ONCE(spin->start) != 0;
 }
 
 /**
@@ -162,7 +162,7 @@ void xe_spin_wait_started(struct xe_spin *spin)
 
 void xe_spin_end(struct xe_spin *spin)
 {
-	spin->end = 0;
+	WRITE_ONCE(spin->end, 0);
 }
 
 /**
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Matthew Brost

Without the READ_ONCE macro is xe_spin_started(), a compiler may choose to read the value of spin->start exactly once rather than every call of xe_spin_started(). If the initial read is before the GPU batch execution flips spin->start to a non-zero value, the test will hang forever. This was shown on Chrome.

For uniformity, update xe_spin_end to use WRITE_ONCE too.

---
  lib/xe/xe_spin.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
lib/xe/xe_spin.c +1 -1
index 0de0b1f2e9..65c29284aa 100644
@@ -145,7 +145,7 @@ void xe_spin_init(struct xe_spin *spin, struct xe_spin_opts *opts)
   */
  bool xe_spin_started(struct xe_spin *spin)
  {
-	return spin->start != 0;
+	return READ_ONCE(spin->start) != 0;
  }
 
  /**

lib/xe/xe_spin.c void xe_spin_wait_started(struct xe_spin *spin) · line 168

Quoted 8 lines
lib/xe/xe_spin.c +1 -1
@@ -162,7 +162,7 @@ void xe_spin_wait_started(struct xe_spin *spin)
 
  void xe_spin_end(struct xe_spin *spin)
  {
-	spin->end = 0;
+	WRITE_ONCE(spin->end, 0);
  }
 
  /**
#3 lore raw
In reply to Matthew Brost
Quoted 18 lines · Matthew Brost

Without the READ_ONCE macro is xe_spin_started(), a compiler may choose to read the value of spin->start exactly once rather than every call of xe_spin_started(). If the initial read is before the GPU batch execution flips spin->start to a non-zero value, the test will hang forever. This was shown on Chrome.

For uniformity, update xe_spin_end to use WRITE_ONCE too.

---
 lib/xe/xe_spin.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/xe/xe_spin.c +0 -0
index 0de0b1f2e9..65c29284aa 100644

lib/xe/xe_spin.c void xe_spin_init(struct xe_spin *spin, struct xe_spin_opts *opts) · line 148

Quoted 5 lines
lib/xe/xe_spin.c +1 -1
@@ -145,7 +145,7 @@ void xe_spin_init(struct xe_spin *spin, struct xe_spin_opts *opts)
  */
 bool xe_spin_started(struct xe_spin *spin)
 {
-	return spin->start != 0;
+	return READ_ONCE(spin->start) != 0;

For dynamic libries in userspace compiler won't optimize the read if function is extern. I had a discussion about it with Lucas some time ago.

Where's the case you observe invalid behavior of this?

--
Zbigniew

Quoted 15 lines
lib/xe/xe_spin.c +1 -1
 }
 
 /**
@@ -162,7 +162,7 @@ void xe_spin_wait_started(struct xe_spin *spin)
 
 void xe_spin_end(struct xe_spin *spin)
 {
-	spin->end = 0;
+	WRITE_ONCE(spin->end, 0);
 }
 
 /**
Signature · 1 line
2.34.1
#4 lore raw
In reply to Matthew Brost
Quoted 5 lines · Matthew Brost

Without the READ_ONCE macro is xe_spin_started(), a compiler may choose to read the value of spin->start exactly once rather than every call of xe_spin_started(). If the initial read is before the GPU batch execution flips spin->start to a non-zero value, the test will hang forever. This was shown on Chrome.

I missed this explanation about Chrome. May you point out the affected code? I'm really curious about how it looks like.

--
Zbigniew

Quoted 33 lines

For uniformity, update xe_spin_end to use WRITE_ONCE too.

---
 lib/xe/xe_spin.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/xe/xe_spin.c +2 -2
index 0de0b1f2e9..65c29284aa 100644
@@ -145,7 +145,7 @@ void xe_spin_init(struct xe_spin *spin, struct xe_spin_opts *opts)
  */
 bool xe_spin_started(struct xe_spin *spin)
 {
-	return spin->start != 0;
+	return READ_ONCE(spin->start) != 0;
 }
 
 /**
@@ -162,7 +162,7 @@ void xe_spin_wait_started(struct xe_spin *spin)
 
 void xe_spin_end(struct xe_spin *spin)
 {
-	spin->end = 0;
+	WRITE_ONCE(spin->end, 0);
 }
 
 /**
Signature · 1 line
2.34.1