Skip to content

mm: replace mm_heap_config_s.heap with an allocheap flag - #21722

Merged
xiaoxiang781216 merged 2 commits into
apache:masterfrom
CV-Bowen:up/P68-mm-allocheap
Nov 12, 2026
Merged

xiaoxiang781216 merged 2 commits into
apache:masterfrom
CV-Bowen:up/P68-mm-allocheap

Conversation

@CV-Bowen

Copy link
Copy Markdown
Contributor

Summary

mm_heap_config_s.heap lets the caller ask mm_initialize_heap() to
allocate the struct mm_heap_s from another heap instead of carving it
out of the new region. The struct is then allocated with
mm_malloc(config->heap, ...) (mm_memalign() in tlsf), so the caller
has to hand in a real struct mm_heap_s *. With
CONFIG_MM_UMM_CUSTOMIZE_MANAGER (sim) the user heap is managed by
sim_ummheap and there is no such struct to pass.

Replace the pointer with a bool allocheap and allocate the struct with
lib_memalign(), which resolves to kmm_memalign() in the kernel and
memalign() otherwise. KRN_HEAP existed only to fill that field for
rptun, so it is removed and rptun sets allocheap = true.

  • mm/mm_heap/mm_initialize: fix nxstyle errors (a missing blank line
    that master already has)
  • mm: replace mm_heap_config_s.heap with an allocheap flag

Impact

  • API: struct mm_heap_config_s loses heap and gains allocheap;
    KRN_HEAP is removed. In tree the only user was drivers/rptun.
    Out-of-tree code that set config.heap must set allocheap = true
    instead.
  • Callers that zero the config (fs_heap, kmm/umm initialize) keep the
    in-region layout, unchanged.
  • No Kconfig change.

Testing

Host: Ubuntu 22.04.5 LTS x86_64, QEMU version 10.2.1, sim: cc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0, qemu-armv8a: aarch64-none-elf-gcc (Arm GNU Toolchain 12.2.Rel1 (Build arm-12.24)) 12.2.1 20221205

rptun is the only in-tree user of the allocate-the-struct path: each
carveout in the resource table gets its own heap through
rptun_init_carveout(). The rpmsg baseline exercises it on both
managers:

  • sim:rpserver + sim:rpproxy: PASS
    • uname -a (server)
    • rpmsg dump all (server)
    • rpmsg ping all 10 1024 3 0 (server)
    • uname -a (proxy)
    • rpmsg ping all 10 1024 3 0 (proxy)
    • mount -t rpmsgfs -o cpu=server,fs=/proc /sp (proxy)
    • cat /sp/version (proxy)
    • rpmsg dump all (proxy)
  • qemu-armv8a:rpserver + qemu-armv8a:rpproxy: PASS
    • uname -a (server)
    • uname -a (proxy)
    • rpmsg dump all (server)
    • rpmsg ping all 10 1024 3 0 (server)
    • rpmsg ping all 10 1024 3 0 (proxy)
    • mount -t rpmsgfs -o cpu=server,fs=/proc /sp (proxy)
    • cat /sp/version (proxy)

Same baseline on sim:rpserver + sim:rpproxy with
CONFIG_MM_TLSF_MANAGER=y (CONFIG_MM_DEFAULT_MANAGER unset) on both
sides: 8/8 pass.

Before/after with the custom user heap manager: sim:rpserver +
sim:rpproxy, both with CONFIG_MM_UMM_CUSTOMIZE_MANAGER=y added, same
baseline cases.

tree result
master server segfaults as soon as the proxy connects; 0/8
this PR 8/8 pass

On master rptun_init_carveout() passes KRN_HEAP (g_mmheap) to
mm_initialize_heap(), which calls mm_malloc() on it. Under the custom
manager g_mmheap points to sim_ummheap's own struct mm_heap_s, so
mm_lock() takes a field of it as a mutex:

master, gdb on sim:rpserver
Program received signal SIGSEGV, Segmentation fault.
0x4001d659 in nxsched_add_prioritized (tcb=0x4014def8, list=0x401151a8 <g_heap+8>) at nuttx/sched/sched/sched.h:512
512	       (next && sched_priority <= next->sched_priority);
#0  0x4001d659 in nxsched_add_prioritized (tcb=0x4014def8, list=0x401151a8 <g_heap+8>) at nuttx/sched/sched/sched.h:512
#1  0x4001eaea in nxsem_wait_slow (sem=0x401151a0 <g_heap>) at nuttx/sched/semaphore/sem_wait.c:266
#2  0x40015305 in nxsem_wait (sem=0x401151a0 <g_heap>) at nuttx/libs/libc/semaphore/sem_wait.c:201
#3  0x4000bb88 in nxmutex_lock (mutex=0x401151a0 <g_heap>) at nuttx/include/nuttx/mutex.h:503
#4  0x4001a51c in mm_lock (heap=0x401151a0 <g_heap>) at nuttx/mm/mm_heap/mm_lock.c:113
#5  0x4001b00d in mm_malloc (heap=0x401151a0 <g_heap>, size=368) at nuttx/mm/mm_heap/mm_malloc.c:215
#6  0x4001a311 in mm_initialize_heap (config=0x401a52b8) at nuttx/mm/mm_heap/mm_initialize.c:262
#7  0x4000a5e8 in rptun_init_carveout (priv=0x4014ddc8, vdev=0x4018500c, shmname=0xf7c17088 "rng0_shm", shmbase=0xf7c17188, shmlen=65312) at nuttx/drivers/rptun/rptun.c:506
#8  0x4000ab19 in rptun_create_device (priv=0x4014ddc8, vdev_=0x401a5390, index=0) at nuttx/drivers/rptun/rptun.c:681
#9  0x4000ad71 in rptun_create_devices (priv=0x4014ddc8) at nuttx/drivers/rptun/rptun.c:829
#10 0x4000b18e in rptun_start_thread (argc=3, argv=0x40194468) at nuttx/drivers/rptun/rptun.c:1022
#11 0x4002484f in nxtask_start () at nuttx/sched/task/task_start.c:99
#12 0x400374d9 in pre_start () at nuttx/arch/sim/src/sim/sim_initialstate.c:54
#13 0x00000000 in ?? ()

Build only: sim:asan (CONFIG_MM_UMM_CUSTOMIZE_MANAGER=y in its
defconfig).

No regression in rpmsg: sim:rpserver/rpproxy and qemu-armv8a:rpserver/rpproxy boot and pass the fixed rpmsg cases (dump, ping, rpmsgfs; 15/15). qemu-armv8a was built with LIBC_ENVPATH and BINFMT_CHECKEXEC disabled, which current master needs for nxinit to start nsh there; unrelated to this change.

Add the blank line after declarations that tools/nxstyle requires,
so later changes to this file pass checkpatch.  No functional change.

Signed-off-by: Bowen Wang <[email protected]>
When mm_heap_config_s.heap is set, mm_initialize_heap() allocates the
struct mm_heap_s from that heap with mm_malloc()/mm_memalign().  With
CONFIG_MM_UMM_CUSTOMIZE_MANAGER (e.g. sim), the user heap is managed
by sim_ummheap and is not a struct mm_heap_s, so the caller cannot
pass a usable heap pointer here.

Replace the heap pointer with a bool allocheap and allocate the heap
struct with lib_memalign(), which picks kmm_memalign() or memalign()
depending on the build.  Drop KRN_HEAP, whose only user was rptun.

Signed-off-by: Bowen Wang <[email protected]>
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: S The size of the change in this PR is small labels Nov 11, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +878 B (+1.3%, 67,865 B / 262,144 B, total: 26% used)

esp32-devkitc

  • ROM: .flash.text +404 B (+0.3%, 130,620 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +404 B (+0.4%, 96,556 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +512 B (+0.6%, 86,980 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +924 B (+1.3%, 70,060 B / 131,072 B, total: 53% used)

qemu-armv8a

  • Code: .text.mm_initialize_heap +8 B, .text.umm_initialize -4 B (+0.0%, 364,132 B)

qemu-intel64

  • Code: .text +832 B (+0.0%, 8,697,320 B)
  • Data: .rodata +96 B (+0.1%, 126,841 B)

rx65n-rsk2mb

  • ROM: .text +368 B (+0.4%, 92,428 B / 2,097,152 B, total: 4% used)

stm32-nucleo-f103rb

  • flash: .text +436 B (+1.2%, 35,800 B / 131,072 B, total: 27% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@xiaoxiang781216
xiaoxiang781216 merged commit f231a24 into apache:master Nov 12, 2026
47 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants