Skip to contentSkip to conversation

gpu: nova: fix incorrect GSP barrier usage

PATCH Applied Gary Guo 3 participants 6 messages Sep 1 – Sep 2

Subsystems (4)
lore
Share & tools
lore

Gary Guo

author
#0 lore raw

v4 cover gpu: nova: fix incorrect GSP barrier usage

Currently Nova GSP code uses Rust barriers, which equivalent to LKMM SMP barriers. This does not provide sufficient ordering for DMA operations on AArch64. Fix this to be using the newly introduced dma_mb(Ordering) API.

Also, there is an incorrect placement of barrier, which is also fixed.

---
Changes in v4:
- Moved barriers to pointer accessor/updater. This does mean unnecessary barrier in failed polling path but the code path is not critical.
I mentioned about providing full acq/rel semantics, but ended up not doing it because LOAD->LOAD barrier is sufficient for the GSP->CPU messaging path, so it's best to still mention about the actually needed ordering only.
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Drop abstraction changes which are already picked.
- Improve ordering comment messages.
- Replace control dependency reliance with explicit barrier.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Dropped Acquire and Release aliases of Full (Joel)
- Use macros to implement most MemoryBarrier
- Split Nova change to GSP->CPU commit and CPU->GSP commit (Joel)
- Link to v1: https://patch.msgid.link/[email protected]

To: Danilo Krummrich <[email protected]>
To: Alice Ryhl <[email protected]>
To: Alexandre Courbot <[email protected]>
To: David Airlie <[email protected]>
To: Simona Vetter <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
Gary Guo (2):

      gpu: nova-core: fix barrier usage in CPU->GSP messaging path
      gpu: nova-core: fix barrier usage in GSP->CPU messaging path
 drivers/gpu/nova-core/gsp/cmdq.rs | 41 ++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 16 deletions(-)

---
base-commit: 89c07d98716a13454ec3fd9f97689e812cc71bd4
change-id: 20260609-rust-barrier-63078ea76216

Best regards,

Signature · 1 line
Gary Guo <[email protected]>
author
#1 lore raw

PATCH v4 1/2 gpu: nova-core: fix barrier usage in CPU->GSP messaging path

In the CPU->GSP messaging path, the code reads the read pointer from GSP, writes the command, advances the write pointer, and then notifies the GSP.

A LOAD->STORE ordering is needed after reading the read pointer from GSP and writing the command. Control dependency exists here which provide the needed ordering, but it's best to avoid depending on it.

A STORE->STORE ordering is needed after the command write and before the write pointer advance. This is currently incorrectly done after the write pointer advance (and before GSP notification), but this can cause issue if GSP is still processing ring buffer, as it may observe the write pointer advance before command write. Thus move this barrier to be before the write pointer advance. Note that barriers are not needed between write pointer advance and GSP notification, as MMIO accessors already carries the required barrier.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +14 -4
index 6da728201281..bfd61e678802 100644
@@ -27,6 +27,11 @@
     ptr,
     sync::{
         aref::ARef,
+        barrier::{
+            dma_mb,
+            Full,
+            Write, //
+        },
         Mutex, //
     },
     time::Delta,
@@ -413,7 +418,12 @@ fn gsp_write_ptr(&self) -> u32 {
     //
     // - The returned value is within `0..MSGQ_NUM_PAGES`.
     fn gsp_read_ptr(&self) -> u32 {
-        MsgqRxHeader::read_ptr(io_project!(self.0, .gspq.rx)) % MSGQ_NUM_PAGES
+        let ptr = MsgqRxHeader::read_ptr(io_project!(self.0, .gspq.rx)) % MSGQ_NUM_PAGES;
+
+        // ORDERING: LOAD->STORE ordering needed to order `gsp_read_ptr` read before data write.
+        dma_mb(Full);
+
+        ptr
     }
 
     // Returns the index of the memory page the CPU can read the next message from.
@@ -447,12 +457,12 @@ fn cpu_write_ptr(&self) -> u32 {
 
     // Informs the GSP that it can process `elem_count` new pages from the command queue.
     fn advance_cpu_write_ptr(&mut self, elem_count: u32) {
+        // ORDERING: STORE->STORE ordering needed to order `cpu_write_ptr` write after data write.
+        dma_mb(Write);
+
         let tx = io_project!(self.0, .cpuq.tx);
         let wptr = MsgqTxHeader::write_ptr(tx).wrapping_add(elem_count) % MSGQ_NUM_PAGES;
         MsgqTxHeader::set_write_ptr(tx, wptr);
-
-        // Ensure all command data is visible before triggering the GSP read.
-        fence(Ordering::SeqCst);
     }
 }
 
Signature · 1 line
2.54.0
Reviewed-by on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] gpu: nova-core: fix barrier usage in CPU->GSP messaging path

Quoted 16 lines · Gary Guo

In the CPU->GSP messaging path, the code reads the read pointer from GSP, writes the command, advances the write pointer, and then notifies the GSP.

A LOAD->STORE ordering is needed after reading the read pointer from GSP and writing the command. Control dependency exists here which provide the needed ordering, but it's best to avoid depending on it.

A STORE->STORE ordering is needed after the command write and before the write pointer advance. This is currently incorrectly done after the write pointer advance (and before GSP notification), but this can cause issue if GSP is still processing ring buffer, as it may observe the write pointer advance before command write. Thus move this barrier to be before the write pointer advance. Note that barriers are not needed between write pointer advance and GSP notification, as MMIO accessors already carries the required barrier.

Quoted 2 lines

---

author
#3 lore raw

PATCH v4 2/2 gpu: nova-core: fix barrier usage in GSP->CPU messaging path

In reply to Gary Guo

In the GSP->CPU messaging path, the code reads the write pointer from GSP, reads the response and advances the read pointer.

A LOAD->LOAD ordering is required after the write pointer read and the data read. Add it as this is currently missing.

A LOAD->STORE ordering is required after the data read and the advance of read pointer. Currently a Rust SeqCst barrier is used, which roughly maps to smp_mb(Full); this however does not order DMA operations (notably on ARM, the generate barrier orders inner shareable and not outer shareable, which is ordered by dma_mb). This ordering does not need to be in between read pointer read and write, because it's for ordering between the ring buffer data and the pointer; the RMW operation does not internally need a barrier (nor it has to be atomic, as CPU pointers are updated by CPU only), so move it to before the RMW sequence for clarity.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +11 -12
index bfd61e678802..14a711307654 100644
@@ -2,13 +2,7 @@
 
 mod continuation;
 
-use core::{
-    mem,
-    sync::atomic::{
-        fence,
-        Ordering, //
-    },
-};
+use core::mem;
 
 use kernel::{
     device,
@@ -30,6 +24,7 @@
         barrier::{
             dma_mb,
             Full,
+            Read,
             Write, //
         },
         Mutex, //
@@ -409,7 +404,12 @@ fn allocate_command(&mut self, size: usize, timeout: Delta) -> Result<GspCommand
     //
     // - The returned value is within `0..MSGQ_NUM_PAGES`.
     fn gsp_write_ptr(&self) -> u32 {
-        MsgqTxHeader::write_ptr(io_project!(self.0, .gspq.tx)) % MSGQ_NUM_PAGES
+        let ptr = MsgqTxHeader::write_ptr(io_project!(self.0, .gspq.tx)) % MSGQ_NUM_PAGES;
+
+        // ORDERING: LOAD->LOAD ordering needed to order `gsp_write_ptr` read before data read.
+        dma_mb(Read);
+
+        ptr
     }
 
     // Returns the index of the memory page the GSP will read the next command from.
@@ -437,12 +437,11 @@ fn cpu_read_ptr(&self) -> u32 {
 
     // Informs the GSP that it can send `elem_count` new pages into the message queue.
     fn advance_cpu_read_ptr(&mut self, elem_count: u32) {
+        // ORDERING: LOAD->STORE ordering needed to order `cpu_read_ptr` write after data read.
+        dma_mb(Full);
+
         let rx = io_project!(self.0, .cpuq.rx);
         let rptr = MsgqRxHeader::read_ptr(rx).wrapping_add(elem_count) % MSGQ_NUM_PAGES;
-
-        // Ensure read pointer is properly ordered.
-        fence(Ordering::SeqCst);
-
         MsgqRxHeader::set_read_ptr(rx, rptr)
     }
 
Signature · 1 line
2.54.0
Reviewed-by on v4 2/2
#4 lore raw

Re: [PATCH v4 2/2] gpu: nova-core: fix barrier usage in GSP->CPU messaging path

Quoted 16 lines · Gary Guo

In the GSP->CPU messaging path, the code reads the write pointer from GSP, reads the response and advances the read pointer.

A LOAD->LOAD ordering is required after the write pointer read and the data read. Add it as this is currently missing.

A LOAD->STORE ordering is required after the data read and the advance of read pointer. Currently a Rust SeqCst barrier is used, which roughly maps to smp_mb(Full); this however does not order DMA operations (notably on ARM, the generate barrier orders inner shareable and not outer shareable, which is ordered by dma_mb). This ordering does not need to be in between read pointer read and write, because it's for ordering between the ring buffer data and the pointer; the RMW operation does not internally need a barrier (nor it has to be atomic, as CPU pointers are updated by CPU only), so move it to before the RMW sequence for clarity.

Quoted 2 lines

---

thanks!

maintainer Applied
#5 lore raw
In reply to Gary Guo
Quoted 1 line · Gary Guo

[PATCH v4 0/2] gpu: nova: fix incorrect GSP barrier usage

Applied, thanks!

Branch: drm-rust-next
Tree: https://gitlab.freedesktop.org/drm/rust/kernel.git

[1/2] gpu: nova-core: fix barrier usage in CPU->GSP messaging path

      commit: 9e270c4fd863
[2/2] gpu: nova-core: fix barrier usage in GSP->CPU messaging path
      commit: 6cb331644c44

The patches will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patches are queued up for the upcoming merge window for the next major kernel release.