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 e5bd5d0dc2233e7d8c1c8eccb655177ef9886350
Author: wangmingrong1 <[email protected]>
AuthorDate: Mon Nov 2 19:00:14 2026 +0800

    libs/libbuiltin: keep the profiler off the traced spinlock helpers
    
    With CONFIG_SMP the gmon lock is a real spinlock whose traced helpers
    are out of line, and thus profiled, when optimization is off. Taking
    the lock through them re-entered mcount_internal() until the stack was
    exhausted, and after guarding that, releasing it through them
    deadlocked on the lock about to be dropped. Use the notrace helpers
    for every lock operation in profile.c.
    
    Signed-off-by: wangmingrong1 <[email protected]>
---
 libs/libbuiltin/libgcc/profile.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/libs/libbuiltin/libgcc/profile.c b/libs/libbuiltin/libgcc/profile.c
index 9a9d38ef54b..264e44650d4 100644
--- a/libs/libbuiltin/libgcc/profile.c
+++ b/libs/libbuiltin/libgcc/profile.c
@@ -294,16 +294,17 @@ void moncontrol(int mode)
       int scale = kcountsize >= textsize ? SCALE_1_TO_1 :
                   (float)kcountsize / textsize * SCALE_1_TO_1;
       FAR unsigned short *kcount = kmm_zalloc(kcountsize);
+
       if (kcount == NULL)
         {
           serr("out of memory\n");
           return;
         }
 
-      flags = spin_lock_irqsave(&p->lock);
+      flags = spin_lock_irqsave_notrace(&p->lock);
       if (p->kcount)
         {
-          spin_unlock_irqrestore(&p->lock, flags);
+          spin_unlock_irqrestore_notrace(&p->lock, flags);
           kmm_free(kcount);
           return;
         }
@@ -314,7 +315,7 @@ void moncontrol(int mode)
       p->textsize = textsize;
       p->kcount = kcount;
       p->kcountsize = kcountsize;
-      spin_unlock_irqrestore(&p->lock, flags);
+      spin_unlock_irqrestore_notrace(&p->lock, flags);
 
       profil(kcount, kcountsize, lowpc, scale);
     }
@@ -322,10 +323,10 @@ void moncontrol(int mode)
     {
       bool running;
 
-      flags = spin_lock_irqsave(&p->lock);
+      flags = spin_lock_irqsave_notrace(&p->lock);
       running = p->running;
       p->running = false;
-      spin_unlock_irqrestore(&p->lock, flags);
+      spin_unlock_irqrestore_notrace(&p->lock, flags);
 
       if (running)
         {
@@ -390,10 +391,10 @@ void monstartup(unsigned long lowpc, unsigned long highpc)
       return;
     }
 
-  flags = spin_lock_irqsave(&p->lock);
+  flags = spin_lock_irqsave_notrace(&p->lock);
   if (p->tos != NULL)
     {
-      spin_unlock_irqrestore(&p->lock, flags);
+      spin_unlock_irqrestore_notrace(&p->lock, flags);
       kmm_free(buffer);
       return;
     }
@@ -408,7 +409,7 @@ void monstartup(unsigned long lowpc, unsigned long highpc)
   p->tos = (FAR struct tostruct *)buffer;
   buffer += p->tossize;
   p->froms = (FAR ARCINDEX *)buffer;
-  spin_unlock_irqrestore(&p->lock, flags);
+  spin_unlock_irqrestore_notrace(&p->lock, flags);
 
   moncontrol(1);
 }
@@ -470,7 +471,13 @@ void mcount_internal(uintptr_t frompc, uintptr_t selfpc)
 
   monstartup((uintptr_t)&_stext, (uintptr_t)&_etext);
 
-  flags = spin_lock_irqsave(&p->lock);
+  /* Every lock user in this file stays off the traced helpers: they are
+   * profiled themselves when they are not inlined, so taking the lock
+   * through them re-enters this function and releasing it through them
+   * deadlocks on the lock about to be dropped.
+   */
+
+  flags = spin_lock_irqsave_notrace(&p->lock);
 
   /* Try next time if fail to initialize for some reason */
 
@@ -576,5 +583,5 @@ void mcount_internal(uintptr_t frompc, uintptr_t selfpc)
     }
 
 done:
-  spin_unlock_irqrestore(&p->lock, flags);
+  spin_unlock_irqrestore_notrace(&p->lock, flags);
 }

Reply via email to