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 220b6727043dcf9ab9c8b162ecdb7640c7290c61
Author: chenzihan1 <[email protected]>
AuthorDate: Mon Aug 17 15:01:42 2026 +0800

    include/ring: fix overcommit in ring_mp_overwrite_start()
    
    Reservation could push prod_head - cons_head past capacity, letting a
    producer write over the oldest live element.
    
    After evicting old data to make room, the loop refreshed old_prod before
    the prod_head CAS.  That defeated the CAS: the space just evicted may
    already have been taken by another producer, and with a refreshed
    expected value the CAS succeeded anyway, reserving on top of the
    eviction instead of retrying.
    
    Two producers A and B, both writing count = 32 bytes into a full ring of
    capacity = 1856, starting at prod_head = P, cons_head = C, P - C = 1856:
    
      #  who  action                                    prod_head  cons_head  
used
      1  A    old_prod = P (read at top of loop)        P          C          
1856
      2  A    free_space = 0, so discard = 32           P          C          
1856
      3  A    CAS cons_head: C -> C+32, succeeds        P          C+32       
1824
      4  A    ring_mc_finish(), cons_tail = C+32        P          C+32       
1824
      5  B    sees free_space = 32 >= 32, skips the
              eviction branch entirely, and
              CAS prod_head: P -> P+32 succeeds         P+32       C+32       
1856
      6  A    re-reads prod_head, gets P+32             P+32       C+32       
1856
      7  A    CAS prod_head: P+32 -> P+64, expected
              value was just read, so it succeeds       P+64       C+32       
1888
    
    Signed-off-by: chenzihan1 <[email protected]>
---
 include/nuttx/ring.h | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/include/nuttx/ring.h b/include/nuttx/ring.h
index 19631fe18ba..eceea564d7f 100644
--- a/include/nuttx/ring.h
+++ b/include/nuttx/ring.h
@@ -1805,9 +1805,6 @@ ring_mp_overwrite_start(FAR struct ring_s *r, uint32_t 
count,
           ring_buf_init(buf, r, cons_idx, discard, elem_size);
           ring_mc_finish(r, buf);
           all_discard += discard;
-
-          old_prod = atomic_read(&blob->prod_head);
-          prod_idx = RTS_IDX(old_prod);
         }
 
       new_prod = RTS_PACK(RTS_CNT(old_prod) + 1,

Reply via email to