This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 81129dfaace8abb5e80f3ebbb64c085528cd2fd5
Author: Bowen Wang <[email protected]>
AuthorDate: Tue Oct 27 11:03:54 2026 +0800

    include/ring: order consumer data reads against the ring indices
    
    ring_sc_start() and ring_sc_peek() read element data right after the
    prod_tail load with no read barrier in between, so a weakly ordered core
    can return bytes older than that prod_tail. ring_sc_finish() published
    cons_tail after UP_WMB(), which orders stores only, so the producer
    could reuse slots still being read.
    
    Move the UP_RMB() in ring_sc_start() and ring_sc_peek() after the
    prod_tail load, and use UP_DMB() before the cons_tail store.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 include/nuttx/ring.h | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/include/nuttx/ring.h b/include/nuttx/ring.h
index 225d861ca3f..9ffb931b039 100644
--- a/include/nuttx/ring.h
+++ b/include/nuttx/ring.h
@@ -1130,8 +1130,11 @@ ring_sc_start(FAR struct ring_s *r, uint32_t count, bool 
bulk,
   uint32_t available;
   uint32_t n = 0;
 
-  UP_RMB();
   prod_index = RTS_IDX(blob->prod_tail);
+
+  /* Read prod_tail before the data it publishes */
+
+  UP_RMB();
   available = ring_dist(r, prod_index, cons_index);
 
   /* available > capacity is physically impossible for a consistent ring;
@@ -1296,7 +1299,9 @@ ring_sc_finish(FAR struct ring_s *r, FAR struct 
ring_buf_s *buf)
     {
       uint32_t cons_index = ring_advance(r, buf->idx, buf->cnt);
 
-      UP_WMB();
+      /* Finish reading the data before handing the slots back */
+
+      UP_DMB();
       blob->cons_tail = RTS_PACK(0, cons_index);
     }
 }
@@ -1330,8 +1335,11 @@ ring_sc_peek(FAR struct ring_s *r, FAR void *items, 
uint32_t count,
       cons_index = ring_clamp_cons(r, *index, cons_index);
     }
 
-  UP_RMB();
   prod_index = RTS_IDX(blob->prod_tail);
+
+  /* Read prod_tail before the data it publishes */
+
+  UP_RMB();
   available = ring_dist(r, prod_index, cons_index);
   n = count < available ? count : available;
 

Reply via email to