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 a1617de7b2f6edc29bef608482add767eeb3d7e3
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Oct 29 22:20:32 2026 +0100

    mm: Do not report the growth of a process heap as a failure.
    
    In the kernel build the heap of a process starts small.  When
    mm_malloc() or mm_memalign() finds no free chunk, the user allocator
    (umm_memalign.c, umm_realloc.c) calls sbrk() and tries again.  But
    mm_malloc() reported each of these failed searches as a failed
    allocation.  With CONFIG_DEBUG_MM_WARN, on qemu-armv8a:knsh a thread
    whose stack made the heap grow gave two "mm_malloc: WARNING: Allocation
    failed, size 24584": the 8 KiB stack must be aligned to 8 KiB
    (CONFIG_TLS_ALIGNED), so mm_memalign() asks mm_malloc() for the size
    and two alignments.  Each other growth of a process heap gave one more.
    With CONFIG_MM_DUMP_ON_FAILURE each growth dumped the heap, and with
    CONFIG_MM_PANIC_ON_FAILURE each growth caused a panic.
    
    MM_INTERNAL_HEAP() now leaves out the heap of a process in the kernel
    build.  The user allocator gives the warning, and the panic of
    CONFIG_MM_PANIC_ON_FAILURE, when sbrk() fails: then the allocation
    fails.
    
    Flat and protected builds do not change.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/nuttx/mm/mm.h      | 19 +++++++++++++++++--
 mm/umm_heap/umm_memalign.c |  8 ++++++++
 mm/umm_heap/umm_realloc.c  |  8 ++++++++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/include/nuttx/mm/mm.h b/include/nuttx/mm/mm.h
index 2dc84918f88..ca0857c3cb0 100644
--- a/include/nuttx/mm/mm.h
+++ b/include/nuttx/mm/mm.h
@@ -133,12 +133,27 @@
 #  define USR_HEAP g_mmheap
 #endif
 
+/* MM_INTERNAL_HEAP() selects the heaps whose failed allocations mm_malloc()
+ * reports.  The heap of a process in the kernel build is not one of them:
+ * it starts small, and a failed allocation makes it grow with sbrk() (see
+ * umm_memalign.c).  The user allocator reports a failure of sbrk() (the
+ * warning and CONFIG_MM_PANIC_ON_FAILURE).
+ */
+
 #ifdef CONFIG_MM_KERNEL_HEAP
 #  define KRN_HEAP               g_kmmheap
-#  define MM_INTERNAL_HEAP(heap) ((heap) == USR_HEAP || (heap) == g_kmmheap)
+#  if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
+#    define MM_INTERNAL_HEAP(heap) ((heap) == g_kmmheap)
+#  else
+#    define MM_INTERNAL_HEAP(heap) ((heap) == USR_HEAP || (heap) == g_kmmheap)
+#  endif
 #else
 #  define KRN_HEAP               USR_HEAP
-#  define MM_INTERNAL_HEAP(heap) ((heap) == USR_HEAP)
+#  if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
+#    define MM_INTERNAL_HEAP(heap) 0
+#  else
+#    define MM_INTERNAL_HEAP(heap) ((heap) == USR_HEAP)
+#  endif
 #endif
 
 #if CONFIG_MM_BACKTRACE >= 0
diff --git a/mm/umm_heap/umm_memalign.c b/mm/umm_heap/umm_memalign.c
index f8ba7e27488..b133e1c0c9c 100644
--- a/mm/umm_heap/umm_memalign.c
+++ b/mm/umm_heap/umm_memalign.c
@@ -26,9 +26,11 @@
 
 #include <nuttx/config.h>
 
+#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
+#include <nuttx/debug.h>
 #include <nuttx/mm/mm.h>
 
 #include "umm_heap/umm_heap.h"
@@ -80,6 +82,12 @@ FAR void *memalign(size_t alignment, size_t size)
           brkaddr = sbrk(size < 1 ? 1 : size);
           if (brkaddr == (FAR void *)-1)
             {
+              /* mm_malloc() does not report a failure in this heap */
+
+              mwarn("WARNING: Allocation failed, size %zu\n", size);
+#ifdef CONFIG_MM_PANIC_ON_FAILURE
+              PANIC();
+#endif
               return NULL;
             }
         }
diff --git a/mm/umm_heap/umm_realloc.c b/mm/umm_heap/umm_realloc.c
index a69a6191ee6..15c7e702986 100644
--- a/mm/umm_heap/umm_realloc.c
+++ b/mm/umm_heap/umm_realloc.c
@@ -26,9 +26,11 @@
 
 #include <nuttx/config.h>
 
+#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
+#include <nuttx/debug.h>
 #include <nuttx/mm/mm.h>
 
 #include "umm_heap/umm_heap.h"
@@ -82,6 +84,12 @@ FAR void *realloc(FAR void *oldmem, size_t size)
           brkaddr = sbrk(size < 1 ? 1 : size);
           if (brkaddr == (FAR void *)-1)
             {
+              /* mm_malloc() does not report a failure in this heap */
+
+              mwarn("WARNING: Allocation failed, size %zu\n", size);
+#ifdef CONFIG_MM_PANIC_ON_FAILURE
+              PANIC();
+#endif
               return NULL;
             }
         }

Reply via email to