rust: more memory barriers bindings
Subsystems (8)
Gary Guo
maintainerv2 cover rust: more memory barriers bindings
This expands the existing smp barriers to also mandatory barriers and DMA barriers.
The API looks like: mb(Ordering)/smp_mb(Ordering)/dma_mb(Ordering), where Ordering is one of Full, Read, Write.
I retain the use of generics to avoid duplicating CONFIG_SMP check (but also retaining the ability to easily add Acquire and Release ordering in the future).
A user of these introduced API is included in patch 3, which is a concurrency bug that exists in Nova code today due to missing barriers.
---
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: Miguel Ojeda <[email protected]>
To: Boqun Feng <[email protected]>
To: Gary Guo <[email protected]>
To: Björn Roy Baron <[email protected]>
To: Benno Lossin <[email protected]>
To: Andreas Hindborg <[email protected]>
To: Alice Ryhl <[email protected]>
To: Trevor Gross <[email protected]>
To: Danilo Krummrich <[email protected]>
To: Will Deacon <[email protected]>
To: Peter Zijlstra <[email protected]>
To: Mark Rutland <[email protected]>
To: Alexandre Courbot <[email protected]>
To: David Airlie <[email protected]>
To: Simona Vetter <[email protected]>
Cc: Alan Stern <[email protected]>
Cc: Andrea Parri <[email protected]>
Cc: Nicholas Piggin <[email protected]>
Cc: David Howells <[email protected]>
Cc: Jade Alglave <[email protected]>
Cc: Luc Maranget <[email protected]>
Cc: "Paul E. McKenney" <[email protected]>
Cc: Akira Yokosawa <[email protected]>
Cc: Daniel Lustig <[email protected]>
Cc: Joel Fernandes <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Gary Guo (4):
rust: sync: add helpers for mb, dma_mb and friends
rust: sync: generic memory barriers
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 | 18 ++++++ drivers/gpu/nova-core/gsp/fw.rs | 12 ---- rust/helpers/barrier.c | 30 +++++++++ rust/kernel/sync/atomic/ordering.rs | 2 +- rust/kernel/sync/barrier.rs | 123 ++++++++++++++++++++++++++++-------- 5 files changed, 144 insertions(+), 41 deletions(-)
---
base-commit: a87737435cfa134f9cdcc696ba3080759d04cf72
change-id: 20260609-rust-barrier-63078ea76216
Best regards,
Signature · 1 line
Gary Guo <[email protected]>
Gary Guo
maintainerPATCH v2 1/4 rust: sync: add helpers for mb, dma_mb and friends
They supplement the existing smp_mb, smp_rmb and smp_wmb.
--- rust/helpers/barrier.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
rust/helpers/barrier.c +30 -0
@@ -2,6 +2,36 @@#include <asm/barrier.h> +__rust_helper void rust_helper_mb(void) +{ +mb (); +} + +__rust_helper void rust_helper_rmb(void) +{ +rmb (); +} + +__rust_helper void rust_helper_wmb(void) +{ +wmb (); +} + +__rust_helper void rust_helper_dma_mb(void) +{ +dma_mb (); +} + +__rust_helper void rust_helper_dma_rmb(void) +{ +dma_rmb (); +} + +__rust_helper void rust_helper_dma_wmb(void) +{ +dma_wmb (); +} + __rust_helper void rust_helper_smp_mb(void) {smp_mb ();
Signature · 1 line
2.54.0
Re: [PATCH v2 1/4] rust: sync: add helpers for mb, dma_mb and friends
Quoted 4 lines · Gary Guo
They supplement the existing
smp_mb,smp_rmbandsmp_wmb.Reviewed-by Eliot Courtney <[email protected]>Signed-off-by Gary Guo <[email protected]>
Queued in rust-sync, thanks!
Regards,
Boqun
[tip: locking/core] rust: sync: Add helpers for mb, dma_mb and friends
In reply to Gary GuoThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 79d3d667af401eaa452f046595209aae6933c485 Gitweb: https://git.kernel.org/tip/79d3d667af401eaa452f046595209aae6933c485 Author: Gary Guo <[email protected]> AuthorDate: Tue, 09 Jun 2026 16:38:37 +01:00 Committer: Boqun Feng <[email protected]> CommitterDate: Tue, 04 Aug 2026 05:52:54 -07:00
rust: sync: Add helpers for mb, dma_mb and friends
They supplement the existing smp_mb, smp_rmb and smp_wmb.
--- rust/helpers/barrier.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
rust/helpers/barrier.c +30 -0
@@ -2,6 +2,36 @@#include <asm/barrier.h> +__rust_helper void rust_helper_mb(void) +{ +mb (); +} + +__rust_helper void rust_helper_rmb(void) +{ +rmb (); +} + +__rust_helper void rust_helper_wmb(void) +{ +wmb (); +} + +__rust_helper void rust_helper_dma_mb(void) +{ +dma_mb (); +} + +__rust_helper void rust_helper_dma_rmb(void) +{ +dma_rmb (); +} + +__rust_helper void rust_helper_dma_wmb(void) +{ +dma_wmb (); +} + __rust_helper void rust_helper_smp_mb(void) {smp_mb ();
Gary Guo
maintainerPATCH v2 2/4 rust: sync: generic memory barriers
In reply to Gary GuoImplement a generic interface for memory barriers (full system/DMA/SMP). The interface uses a parameter to force user to specify their intent with barriers.
Provide Read, Write, Full orderings which map to the existing rmb(), wmb() and mb(). Generic is used here instead of providing individual standalone functions to reduce code duplication; for example, the CONFIG_SMP check in smp_mb is uniformly implemented for all SMP barriers. This could extend to virt_mb's if they're introduced in the future. It would also make it easier if new ordering types are introduced in the future (e.g. Acquire, Release).
--- rust/kernel/sync/atomic/ordering.rs | 2 +- rust/kernel/sync/barrier.rs | 123 ++++++++++++++++++++++++++++-------- 2 files changed, 96 insertions(+), 29 deletions(-)
rust/kernel/sync/atomic/ordering.rs +1 -1
@@ -15,7 +15,7 @@//! - It provides ordering between the annotated operation and all the following memory accesses. //! - It provides ordering between all the preceding memory accesses and all the following memory //! accesses. -//! - All the orderings are the same strength as a full memory barrier (i.e. `smp_mb()`). +//! - All the orderings are the same strength as a full memory barrier (i.e. `smp_mb(Full)`). //! - [`Relaxed`] provides no ordering except the dependency orderings. Dependency orderings are //! described in "DEPENDENCY RELATIONS" in [`LKMM`]'s [`explanation`]. //!
rust/kernel/sync/barrier.rs +95 -28
144 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 2/4] rust: sync: generic memory barriers
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The import statement `pub use ordering::{Full, Read, Write};` violates the vertical import style…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The import statement pub use ordering::{Full, Read, Write}; violates the vertical import style guideline. --
commit c070c38a2a2dec94e13a6201bf19eb33e82b5478
Author: Gary Guo <[email protected]>
rust: sync: generic memory barriers
This commit implements a generic interface for memory barriers (full system, DMA, and SMP) that requires the user to specify their intent. It provides Read, Write, and Full orderings which map to the existing rmb(), wmb(), and mb() barriers to reduce code duplication.
Quoted 4 lines
rust/kernel/sync/barrier.rs +0
-0
[ ... ]
rust/kernel/sync/barrier.rs line 33
Quoted 11 lines
rust/kernel/sync/barrier.rs +11
-0+pub mod ordering { +pub use crate ::sync::atomic::ordering::Full ; + +/// The annotation type for read-read barrier ordering. +pub struct Read ; + +/// The annotation type for write-write barrier ordering. +pub struct Write ; +} + +pub use ordering::{Full ,Read ,Write };
[Severity: Low]
This isn't a bug, but does this violate the kernel's vertical import style guidelines?
The subsystem documentation states that commits touching imports should follow the vertical import style, which places each imported item on its own line when braces are used to group multiple items.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Boqun Feng
maintainerRe: [PATCH v2 2/4] rust: sync: generic memory barriers
In reply to Gary GuoQuoted 4 lines · Gary Guo
Implement a generic interface for memory barriers (full system/DMA/SMP). The interface uses a parameter to force user to specify their intent with barriers.
Quoted 8 lines
Provide
Read,Write,Fullorderings which map to the existingrmb(),wmb()andmb(). Generic is used here instead of providing individual standalone functions to reduce code duplication; for example, theCONFIG_SMPcheck insmp_mbis uniformly implemented for all SMP barriers. This could extend tovirt_mb's if they're introduced in the future. It would also make it easier if new ordering types are introduced in the future (e.g.Acquire,Release).
Queued with minor changes:
* Added "Add" into the commit title.
* Applied vertical import style.
But I do want to get more memory model reviews on this patch series, so please do take a look if you're interested ;-)
Regards,
Boqun
Quoted 173 lines
Signed-off-by Gary Guo <[email protected]>--- rust/kernel/sync/atomic/ordering.rs | 2 +- rust/kernel/sync/barrier.rs | 123 ++++++++++++++++++++++++++++-------- 2 files changed, 96 insertions(+), 29 deletions(-)rust/kernel/sync/atomic/ordering.rs +1
-1@@ -15,7 +15,7 @@//! - It provides ordering between the annotated operation and all the following memory accesses. //! - It provides ordering between all the preceding memory accesses and all the following memory //! accesses. -//! - All the orderings are the same strength as a full memory barrier (i.e. `smp_mb()`). +//! - All the orderings are the same strength as a full memory barrier (i.e. `smp_mb(Full)`). //! - [`Relaxed`] provides no ordering except the dependency orderings. Dependency orderings are //! described in "DEPENDENCY RELATIONS" in [`LKMM`]'s [`explanation`]. //! rust/kernel/sync/barrier.rs +95
-28@@ -7,6 +7,34 @@//! //! [`LKMM`]: srctree/tools/memory-model/ +#![expect(private_bounds, reason = "sealed implementation" )] + +/// Memory barrier orderings. +/// +/// The semantics of these orderings follows the [`LKMM`] definitions and rules. +/// +/// - [`Read`] provides ordering between preceding load operations and succeeding load operations. +/// - [`Write`] provides ordering between preceding store operations and succeeding store +/// operations. +/// - [`Full`] provides ordering between all the preceding memory accesses and succeeding memory +/// accesses. +/// +/// [`LKMM`]: srctree/tools/memory-model/ +pub mod ordering { +pub use crate ::sync::atomic::ordering::Full ; + +/// The annotation type for read-read barrier ordering. +pub struct Read ; + +/// The annotation type for write-write barrier ordering. +pub struct Write ; +} + +pub use ordering::{Full ,Read ,Write }; + +struct Smp ; +struct Dma ; +/// A compiler barrier. /// /// A barrier that prevents compiler from reordering memory accesses across the barrier. @@ -19,43 +47,82 @@ pub(crate) fn barrier() {unsafe { core::arch::asm!("" ) }; } -/// A full memory barrier. +trait MemoryBarrier <Flavour = ()> { +fn run (); +} + +macro_rules! define_barrier { + ($([$flavour:ident])? $ordering:ident, $binding:ident) => { +impl MemoryBarrier $(<$flavour>)?for $ordering { +#[inline] +fn run () { +// SAFETY: barrier methods are safe to call. +unsafe { bindings::$binding() }; + } + } + }; +} + +define_barrier! (Full , mb); +define_barrier! (Read , rmb); +define_barrier! (Write , wmb); +define_barrier! ([Dma ]Full , dma_mb); +define_barrier! ([Dma ]Read , dma_rmb); +define_barrier! ([Dma ]Write , dma_wmb); +define_barrier! ([Smp ]Full , smp_mb); +define_barrier! ([Smp ]Read , smp_rmb); +define_barrier! ([Smp ]Write , smp_wmb); + +/// Memory barrier. /// /// A barrier that prevents compiler and CPU from reordering memory accesses across the barrier. -#[inline(always)] -pub fn smp_mb () { -if cfg! (CONFIG_SMP ) { -// SAFETY: `smp_mb()` is safe to call. -unsafe { bindings::smp_mb () }; - }else { -barrier (); - } +/// +/// The specific forms of reordering can be specified using the parameter. +/// - `mb(Read)` provides a read-read barrier. +/// - `mb(Write)` provides a write-write barrier. +/// - `mb(Full)` provides a full barrier. +/// +/// # Examples +/// +/// ``` +/// # use kernel::sync::barrier::*; +/// mb(Read); +/// mb(Write); +/// mb(Full); +/// ``` +#[inline] +#[doc(alias = "rmb" )] +#[doc(alias = "wmb" )] +pub fn mb <T :MemoryBarrier >(_:T ) { +T ::run () } -/// A write-write memory barrier. +/// Memory barrier between CPUs. /// -/// A barrier that prevents compiler and CPU from reordering memory write accesses across the -/// barrier. -#[inline(always)] -pub fn smp_wmb () { +/// A barrier that prevents compiler and CPU from reordering memory accesses across the barrier. +/// Does not prevent re-ordering with respect to other bus-mastering devices. +/// +/// See [`mb`] for usage. +#[inline] +#[doc(alias = "smp_rmb" )] +#[doc(alias = "smp_wmb" )] +pub fn smp_mb <T :MemoryBarrier <Smp >>(_:T ) {if cfg! (CONFIG_SMP ) { -// SAFETY: `smp_wmb()` is safe to call. -unsafe { bindings::smp_wmb () }; +T ::run () }else { -barrier (); +barrier () } } -/// A read-read memory barrier. +/// Memory barrier between local CPU and bus-mastering devices. /// -/// A barrier that prevents compiler and CPU from reordering memory read accesses across the -/// barrier. -#[inline(always)] -pub fn smp_rmb () { -if cfg! (CONFIG_SMP ) { -// SAFETY: `smp_rmb()` is safe to call. -unsafe { bindings::smp_rmb () }; - }else { -barrier (); - } +/// A barrier that prevents compiler and CPU from reordering memory accesses across the barrier. +/// Does not prevent re-ordering with respect to other CPUs. +/// +/// See [`mb`] for usage. +#[inline] +#[doc(alias = "dma_rmb" )] +#[doc(alias = "dma_wmb" )] +pub fn dma_mb <T :MemoryBarrier <Dma >>(_:T ) { +T ::run () }Signature · 2 lines
-- 2.54.0
[tip: locking/core] rust: sync: Add generic memory barriers
In reply to Gary GuoThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 72856afd33f2fa166c06f1536003e300e51ecf09 Gitweb: https://git.kernel.org/tip/72856afd33f2fa166c06f1536003e300e51ecf09 Author: Gary Guo <[email protected]> AuthorDate: Tue, 09 Jun 2026 16:38:38 +01:00 Committer: Boqun Feng <[email protected]> CommitterDate: Tue, 04 Aug 2026 05:52:54 -07:00
rust: sync: Add generic memory barriers
Implement a generic interface for memory barriers (full system/DMA/SMP). The interface uses a parameter to force user to specify their intent with barriers.
Provide Read, Write, Full orderings which map to the existing rmb(), wmb() and mb(). Generic is used here instead of providing individual standalone functions to reduce code duplication; for example, the CONFIG_SMP check in smp_mb is uniformly implemented for all SMP barriers. This could extend to virt_mb's if they're introduced in the future. It would also make it easier if new ordering types are introduced in the future (e.g. Acquire, Release).
--- rust/kernel/sync/atomic/ordering.rs | 2 +- rust/kernel/sync/barrier.rs | 127 +++++++++++++++++++++------ 2 files changed, 100 insertions(+), 29 deletions(-)
rust/kernel/sync/atomic/ordering.rs +1 -1
@@ -15,7 +15,7 @@//! - It provides ordering between the annotated operation and all the following memory accesses. //! - It provides ordering between all the preceding memory accesses and all the following memory //! accesses. -//! - All the orderings are the same strength as a full memory barrier (i.e. `smp_mb()`). +//! - All the orderings are the same strength as a full memory barrier (i.e. `smp_mb(Full)`). //! - [`Relaxed`] provides no ordering except the dependency orderings. Dependency orderings are //! described in "DEPENDENCY RELATIONS" in [`LKMM`]'s [`explanation`]. //!
rust/kernel/sync/barrier.rs +99 -28
148 lines not on this page · raw message
Gary Guo
maintainerPATCH v2 3/4 gpu: nova-core: fix barrier usage in CPU->GSP messaging path
In reply to Gary GuoIn 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. A barrier is not needed here because control dependency provides the required ordering. Still, document it explicitly.
A STORE->STORE ordering is needed after the command write and 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 | 10 ++++++++++ drivers/gpu/nova-core/gsp/fw.rs | 3 --- 2 files changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +10 -0
@@ -20,6 +20,10 @@ ptr, sync::{ aref::ARef, + barrier::{ + dma_mb, + Write, // + }, Mutex, // }, time::Delta,
@@ -274,6 +278,9 @@ fn new(dev: &device::Device<device::Bound>) -> Result<Self> { (rx -1 ,0 ) }; +// ORDERING: control dependency provides necessary LOAD->STORE ordering. +// `dma_mb(Full)` may be used here if we don't want to rely on control dependency. +// SAFETY: // - `data` was created from a valid pointer, and `rx` and `tx` are in the // `0..MSGQ_NUM_PAGES` range per the invariants of `cpu_write_ptr` and `gsp_read_ptr`,
@@ -443,6 +450,9 @@ 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: Ensure all command data is visible before updateing ring buffer pointer. +dma_mb (Write ); +super ::fw::gsp_mem::advance_cpu_write_ptr (&self.0 , elem_count) } }
drivers/gpu/nova-core/gsp/fw.rs +0 -3
@@ -91,9 +91,6 @@ pub(in crate::gsp) fn advance_cpu_write_ptr(qs: &Coherent<GspMem>, count: u32) {let wptr =cpu_write_ptr (qs).wrapping_add (count) %MSGQ_NUM_PAGES ;dma_write! (qs, .cpuq.tx.0 .writePtr, wptr); - -// Ensure all command data is visible before triggering the GSP read. -fence (Ordering ::SeqCst ); } }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 3/4] gpu: nova-core: fix barrier usage in CPU->GSP messaging path
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing sequence number validation allows mismatched RPC replies to be accepted after a timeout. --
commit 0c5cf61612018cf7b218d6031a8329297e4ac9dc
Author: Gary Guo <[email protected]>
gpu: nova-core: fix barrier usage in CPU->GSP messaging path
This commit fixes the barrier usage in the CPU->GSP messaging path. It clarifies the LOAD->STORE ordering dependency and moves the STORE->STORE barrier to immediately before the write pointer advance to prevent the GSP from observing an advanced pointer before the command is fully written.
drivers/gpu/nova-core/gsp/cmdq.rs
Quoted 4 lines
drivers/gpu/nova-core/gsp/cmdq.rs +0
-0
[ ... ]
[Severity: High]
This isn't a bug introduced by this patch, but while looking at the messaging path, I noticed a potential issue with sequence number validation.
If a previous command times out in Cmdq::send_command():
drivers/gpu/nova-core/gsp/cmdq.rs:Cmdq::send_command() {
...
loop {
match inner.receive_msg::<M::Reply>(Self::RECEIVE_TIMEOUT) {
Ok(reply) => break Ok(reply),
Err(ERANGE) => continue,
Err(e) => break Err(e),
}
}
...
}The timeout returns ETIMEDOUT and drops the command queue lock, but the delayed reply might eventually arrive and remain in the message queue.
When another command with the same function code is subsequently sent, CmdqInner::receive_msg() validates the incoming message:
drivers/gpu/nova-core/gsp/cmdq.rs:CmdqInner::receive_msg() {
...
let result = if function == M::FUNCTION {
let (cmd, contents_1) = M::Message::from_bytes_prefix(...
...
}Does this code incorrectly accept the stale reply as a successful response because it only checks if function == M::FUNCTION?
Could this break RPC protocol synchronization and cause the driver to process incorrect payload data? Should the sequence number be validated (e.g., via header.sequence()) to prevent mismatched replies?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Gary Guo
maintainerPATCH v2 4/4 gpu: nova-core: fix barrier usage in GSP->CPU messaging path
In reply to Gary GuoIn the CPU->GSP 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 out to cmdq.rs for clarity.
--- drivers/gpu/nova-core/gsp/cmdq.rs | 8 ++++++++ drivers/gpu/nova-core/gsp/fw.rs | 9 --------- 2 files changed, 8 insertions(+), 9 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +8 -0
@@ -22,6 +22,8 @@ aref::ARef, barrier::{ dma_mb, + Full, + Read, Write, // }, Mutex, //
@@ -339,6 +341,9 @@ fn driver_write_area_size(&self) -> usize { (MSGQ_NUM_PAGES , tx) }; +// ORDERING: Ensure data load is ordered after load of GSP write pointer. +dma_mb (Read ); +// SAFETY: // - `data` was created from a valid pointer, and `rx` and `tx` are in the // `0..MSGQ_NUM_PAGES` range per the invariants of `gsp_write_ptr` and `cpu_read_ptr`,
@@ -436,6 +441,9 @@ 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: Ensure read pointer is properly ordered with a LOAD->STORE ordering. +dma_mb (Full ); +super ::fw::gsp_mem::advance_cpu_read_ptr (&self.0 , elem_count) }
drivers/gpu/nova-core/gsp/fw.rs +0 -9
@@ -46,11 +46,6 @@// TODO: Replace with `IoView` projections once available. pub (super )mod gsp_mem { -use core::sync::atomic::{ - fence, -Ordering ,// - }; -use kernel::{ dma::Coherent , dma_read,
@@ -76,10 +71,6 @@ pub(in crate::gsp) fn cpu_read_ptr(qs: &Coherent<GspMem>) -> u32 {pub (in crate ::gsp)fn advance_cpu_read_ptr (qs: &Coherent <GspMem >, count:u32 ) {let rptr =cpu_read_ptr (qs).wrapping_add (count) %MSGQ_NUM_PAGES ; - -// Ensure read pointer is properly ordered. -fence (Ordering ::SeqCst ); -dma_write! (qs, .cpuq.rx.0 .readPtr, rptr); }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 4/4] gpu: nova-core: fix barrier usage in GSP->CPU messaging path
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Early returns in receive_msg skip advancing the GSP read pointer, leading to a permanently stalled command queue.
- [High] CPU->GSP messaging path relies on a fragile control dependency for memory ordering instead of an explicit DMA barrier. --
commit 4410cc89c9104f4a90dab2676e5da7ce4fd1a204
Author: Gary Guo <[email protected]>
gpu: nova-core: fix barrier usage in GSP->CPU messaging path
This commit replaces the Rust SeqCst barrier with explicit dma_mb(Read) and dma_mb(Full) barriers in the GSP->CPU messaging path. The SeqCst barrier does not correctly order DMA operations on architectures like ARM, so using the proper dma_mb() ensures correct LOAD->LOAD and LOAD->STORE ordering.
Quoted 4 lines
drivers/gpu/nova-core/gsp/cmdq.rs +0
-0
[ ... ]
drivers/gpu/nova-core/gsp/cmdq.rs fn driver_write_area_size(&self) -> usize { · line 346
Quoted 7 lines
drivers/gpu/nova-core/gsp/cmdq.rs +3
-0@@ -339,6 +341,9 @@ fn driver_write_area_size(&self) -> usize { (MSGQ_NUM_PAGES , tx) }; +// ORDERING: Ensure data load is ordered after load of GSP write pointer. +dma_mb (Read ); +
[Severity: High]
This is a pre-existing issue, but since we are updating memory barriers here, does the CPU->GSP messaging path in driver_write_area() also need an explicit barrier instead of relying on a fragile control dependency?
drivers/gpu/nova-core/gsp/cmdq.rs:driver_write_area() {
...
let (tail_end, wrap_end) = if rx == 0 {
...
} else if rx <= tx {
...
} else {
...
};
// ORDERING: control dependency provides necessary LOAD->STORE ordering.
// dma_mb(Full) may be used here if we don't want to rely on control dependency.
...
}Relying on control dependencies for DMA memory ordering is unsafe in Rust, as the compiler does not guarantee preserving them and may optimize away branches (e.g., using conditional selects like csel on ARM64).
Control dependencies also do not emit architectural barriers (like dmb osh on ARM64), which are necessary to order memory accesses across the system interconnect for device memory.
If the CPU's store to the command buffer is observed by the GSP before the CPU's load of rx, could the CPU overwrite data the GSP has not yet consumed?
Should an explicit dma_mb(Full) be added here to guarantee LOAD->STORE ordering?
Quoted 3 lines
drivers/gpu/nova-core/gsp/cmdq.rs +0
-0// SAFETY: // - `data` was created from a valid pointer, and `rx` and `tx` are in the // `0..MSGQ_NUM_PAGES` range per the invariants of `gsp_write_ptr` and `cpu_read_ptr`,
[ ... ]
drivers/gpu/nova-core/gsp/cmdq.rs fn cpu_read_ptr(&self) -> u32 { · line 448
Quoted 9 lines
drivers/gpu/nova-core/gsp/cmdq.rs +3
-0@@ -436,6 +441,9 @@ 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: Ensure read pointer is properly ordered with a LOAD->STORE ordering. +dma_mb (Full ); +super ::fw::gsp_mem::advance_cpu_read_ptr (&self.0 , elem_count) }
[Severity: High]
This is a pre-existing issue, but looking at how advance_cpu_read_ptr() is used, do the early returns in receive_msg() skip advancing the GSP read pointer?
drivers/gpu/nova-core/gsp/cmdq.rs:receive_msg() {
...
let function = message.header.function().map_err(|_| EINVAL)?;
// Extract the message. Store the result as we want to advance the read pointer even in
// case of failure.
let result = if function == M::FUNCTION {
let (cmd, contents_1) = M::Message::from_bytes_prefix(message.contents.0).ok_or(EIO)?;
...
// Advance the read pointer past this message.
self.gsp_mem.advance_cpu_read_ptr(...);
...
}The comments note that the result is stored to ensure the read pointer is advanced even on failure. However, the use of the ? operator on message.header.function() and M::Message::from_bytes_prefix() causes an immediate return.
If an early return happens here, will subsequent polls read the exact same malformed message?
Could this cause repeated failures and a permanently stalled command queue?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4