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 f2909e04934f4ff661179d20b662d7a3bd00669c
Author: chenzihan1 <[email protected]>
AuthorDate: Tue Aug 25 10:04:43 2026 +0800

    include/ring: drop the request instead of spinning when eviction is capped
    
    ring_mp_overwrite_start() spun forever in an interrupt handler, wedging
    the CPU.
    
    When the ring is full the reservation has to evict old data first, bounded
    by the committed region: dist(prod_tail, cons_head).  Slots between
    prod_tail and prod_head belong to producers that reserved them but have
    not called ring_mp_finish() yet, so evicting them would hand out memory
    another producer is writing into.  The loop therefore retried until enough
    data had been committed.
    
    That retry assumes the in-flight producers can make progress.  The note
    path is reentrant and does not disable interrupts, so they may not: a
    producer preempted between its prod_head CAS and ring_mp_finish() cannot
    run while an interrupt handler on the same CPU spins waiting for it.
    prod_tail stays frozen, committed only shrinks as later producers evict,
    and the retry never terminates.
    
    Observed on TriCore during boot, with a note of 20 bytes on a ring of
    capacity 1856:
    
      prod_head = 0x763cc545  cnt=118  idx=3982661
      prod_tail = 0x753cbded  cnt=117  idx=3980781
      cons_head = 0xc83cbde9  cnt=200  idx=3980777
      cons_tail = 0xc83cbde9  cnt=200  idx=3980777
    
      free_space = 0, committed = dist(prod_tail, cons_head) = 4,
      discard = 20 > 4
    
    The RTS counters are consistent here - prod skew is 1, matching the single
    in-flight producer - so nothing is corrupt; the loop simply has no exit.
    
    Drop the request instead.  Trace records are discardable, and the whole
    count is reported as discarded so CONFIG_DRIVERS_NOTE_STATS accounts for
    it.  Returning an empty buf makes ring_mp_finish() return early, leaving
    no prod_head reservation unmatched.  Once the stalled producer completes,
    prod_tail catches up and the ring resumes accepting writes.
    
    ring_sp_overwrite_start() is single-producer, so prod_head never runs
    ahead of prod_tail there and it needs no equivalent change.
    
    Signed-off-by: chenzihan1 <[email protected]>
---
 include/nuttx/ring.h | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/include/nuttx/ring.h b/include/nuttx/ring.h
index 0e9a611c079..daabe3c615a 100644
--- a/include/nuttx/ring.h
+++ b/include/nuttx/ring.h
@@ -1781,8 +1781,22 @@ ring_mp_overwrite_start(FAR struct ring_s *r, uint32_t 
count,
           discard = count - free_space;
           if (discard > committed)
             {
-              old_prod = atomic_read(&blob->prod_head);
-              continue;
+              /* Not enough committed data to evict: the room we would need
+               * is still held by producers that reserved it but have not
+               * called ring_mp_finish() yet, and evicting it would hand out
+               * slots another producer is writing into.
+               *
+               * Spinning here would wait for those producers to finish, but
+               * this path is reentrant - a producer preempted between its
+               * prod_head CAS and ring_mp_finish() cannot make progress
+               * while an interrupt handler on the same CPU spins for it.
+               * Drop the request instead: the whole count is reported as
+               * discarded and an empty buf makes ring_mp_finish() return
+               * early, so no prod_head reservation is left unmatched.
+               */
+
+              ring_buf_init(buf, r, 0, 0, elem_size);
+              return all_discard + count;
             }
 
           new_cons = RTS_PACK(RTS_CNT(old_cons) + 1,

Reply via email to